init method

void init({
  1. bool useDefaultModuleAndBinding = true,
  2. HTLocale? locale,
  3. Map<String, Function> externalFunctions = const {},
  4. Map<String, HTExternalFunctionTypedef> externalFunctionTypedef = const {},
  5. List<HTExternalClass> externalClasses = const [],
  6. List<HTExternalTypeReflection> externalTypeReflections = const [],
})

Initialize the interpreter, prepare it with preincluded modules, bind it with HTExternalFunction, HTExternalFunctionTypedef, HTExternalClass, etc.

Implementation

void init({
  bool useDefaultModuleAndBinding = true,
  HTLocale? locale,
  Map<String, Function> externalFunctions = const {},
  Map<String, HTExternalFunctionTypedef> externalFunctionTypedef = const {},
  List<HTExternalClass> externalClasses = const [],
  List<HTExternalTypeReflection> externalTypeReflections = const [],
}) {
  if (_isInitted) return;

  if (locale != null) {
    HTLocale.current = locale;
  }

  if (useDefaultModuleAndBinding) {
    // bind externals before any eval
    for (var key in preincludeFunctions.keys) {
      interpreter.bindExternalFunction(key, preincludeFunctions[key]!);
    }
    interpreter.bindExternalClass(HTNumberClassBinding());
    interpreter.bindExternalClass(HTIntClassBinding());
    interpreter.bindExternalClass(HTBigIntClassBinding());
    interpreter.bindExternalClass(HTFloatClassBinding());
    interpreter.bindExternalClass(HTBooleanClassBinding());
    interpreter.bindExternalClass(HTStringClassBinding());
    interpreter.bindExternalClass(HTIteratorClassBinding());
    interpreter.bindExternalClass(HTIterableClassBinding());
    interpreter.bindExternalClass(HTListClassBinding());
    interpreter.bindExternalClass(HTSetClassBinding());
    interpreter.bindExternalClass(HTMapClassBinding());
    interpreter.bindExternalClass(HTRandomClassBinding());
    interpreter.bindExternalClass(HTMathClassBinding());
    interpreter.bindExternalClass(HTHashClassBinding());
    interpreter.bindExternalClass(HTSystemClassBinding());
    interpreter.bindExternalClass(HTFutureClassBinding());
    // bindExternalClass(HTConsoleClass());
    interpreter.bindExternalClass(HTHetuClassBinding());
    // load precompiled core module.
    final coreModule = Uint8List.fromList(hetuCoreModule);
    interpreter.loadBytecode(
      bytes: coreModule,
      moduleName: 'hetu',
      globallyImport: true,
      printPerformanceStatistics: config.printPerformanceStatistics,
    );

    interpreter.define('kHetuVersion', kHetuVersion.toString());

    interpreter.invoke('initHetuEnv', positionalArgs: [this]);

    HTInterpreter.rootClass = interpreter.globalNamespace
        .memberGet(lexicon.globalObjectId, isRecursive: true);
    HTInterpreter.rootStruct = interpreter.globalNamespace
        .memberGet(lexicon.globalPrototypeId, isRecursive: true);
  }

  for (final key in externalFunctions.keys) {
    interpreter.bindExternalFunction(key, externalFunctions[key]!);
  }
  for (final key in externalFunctionTypedef.keys) {
    interpreter.bindExternalFunctionType(key, externalFunctionTypedef[key]!);
  }
  for (final value in externalClasses) {
    interpreter.bindExternalClass(value);
  }
  for (final value in externalTypeReflections) {
    interpreter.bindExternalReflection(value);
  }
  _isInitted = true;
}