runScript function

Future<int> runScript(
  1. String script
)

Run a given input string in console in native code via dart ffi

Implementation

Future<int> runScript(String script) async {
  final resolvedPackageUri = await Isolate.resolvePackageUri(Uri.parse(packageUri));
  if (resolvedPackageUri == null) {
    throw DerryError(
      type: ErrorCode.invalidPackageUri,
      body: {'packageUri': packageUri},
    );
  }

  final objectFilePath = resolvedPackageUri.resolve(path.join(blobsPath, getBlobFilename())).toFilePath();
  late ffi.DynamicLibrary dylib;
  try {
    dylib = ffi.DynamicLibrary.open(objectFilePath);
  } catch (e) {
    throw DerryError(
      type: ErrorCode.invalidBlob,
      body: {'path': objectFilePath, 'origin': e},
    );
  }

  final nativeRunScriptFn = dylib
      .lookup<ffi.NativeFunction<ffi.Int32 Function(ffi.Pointer<Utf8>)>>('run_script')
      .asFunction<int Function(ffi.Pointer<Utf8>)>();

  return nativeRunScriptFn(script.toNativeUtf8());
}