create method

  1. @override
int create(
  1. String code, {
  2. String? externalFunctions,
  3. String? scriptName,
})
override

Creates a handle from Python code.

If externalFunctions is non-null, it is a comma-separated list of external function names.

If scriptName is non-null, it overrides the default filename used in tracebacks and error messages.

Returns the handle address as an int, or throws on error.

Implementation

@override
int create(String code, {String? externalFunctions, String? scriptName}) {
  final cCode = code.toNativeUtf8().cast<Char>();
  final nullChar = nullptr.cast<Char>();
  final cExtFns = externalFunctions != null
      ? externalFunctions.toNativeUtf8().cast<Char>()
      : nullChar;
  final cScriptName = scriptName != null
      ? scriptName.toNativeUtf8().cast<Char>()
      : nullChar;
  final outError = calloc<Pointer<Char>>();

  try {
    final handle = ffi_native.monty_create(
      cCode,
      cExtFns,
      cScriptName,
      outError,
    );
    if (handle == nullptr) {
      final errorMsg = _readAndFreeString(outError.value);
      throw MontyException(message: errorMsg ?? 'monty_create returned null');
    }

    return handle.address;
  } finally {
    calloc.free(cCode);
    if (externalFunctions != null) calloc.free(cExtFns);
    if (scriptName != null) calloc.free(cScriptName);
    calloc.free(outError);
  }
}