JSObject.makeFunction constructor

JSObject.makeFunction(
  1. JSContext context, {
  2. String? name,
  3. List<String>? parameterNames,
  4. required String body,
  5. String? sourceURL,
  6. int startingLineNumber = 0,
  7. bool autoDispose = true,
})

Creates a function with a given script as its body. Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution. name A JSString containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function. parameterNames (JSStringRef[]) A JSString array containing the names of the function's parameters. Pass NULL if parameterCount is 0. body A JSString containing the script to use as the function's body. sourceURL A JSString containing a URL for the script's source file. This is only used when reporting exceptions. Pass NULL if you do not care to include source file information in exceptions. startingLineNumber (int) An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1. exception (JSValueRef*) A pointer to a JSValueRef in which to store a syntax error exception, if any. Pass NULL if you do not care to store a syntax error exception.

Implementation

JSObject.makeFunction(
  this.context, {
  String? name,
  List<String>? parameterNames,
  required String body,
  String? sourceURL,
  int startingLineNumber = 0,
  bool autoDispose = true,
}) {
  final JSException exception = JSException.create(context);
  final JSString? jsname = name != null ? JSString.fromString(name) : null;
  final List<JSString>? jsParameterNames =
      parameterNames?.map((e) => JSString.fromString(e)).toList();
  final JSString jsbody = JSString.fromString(body);
  final JSString? jssourceURL =
      sourceURL != null ? JSString.fromString(sourceURL) : null;
  _ref = JSObjectMakeFunction(
    context.ref,
    jsname?.ref ?? nullptr,
    jsParameterNames?.length ?? 0,
    jsParameterNames?.ref ?? nullptr,
    jsbody.ref,
    jssourceURL?.ref ?? nullptr,
    startingLineNumber,
    exception.ref,
  );
  if (exception.shouldThrow) throw exception.error;
  attach(calloc.nativeFree, _ref.cast(), autoDispose: autoDispose);
}