execute method

  1. @override
Future<ExecuteResult> execute(
  1. String source, {
  2. String? basePath,
})
override

Execute source code as a fresh program. Maps to: .execute <path> (when given source) or D4rt.execute()

Creates a new environment, only bridged classes available. basePath specifies the directory for resolving imports in the source. If basePath is null, uses cwd.

Implementation

@override
Future<ExecuteResult> execute(String source, {String? basePath}) async {
  try {
    // `execute` means "run as a fresh program", and `_d4rt.execute` parses
    // its argument as a compilation unit — so a bare statement list has to be
    // wrapped in a `main` first. No import block: unlike the REPL, this tier
    // does not own the bridge registration, so it must not inject imports.
    final result = await _d4rt.execute(
      source: prepareProgramSource(source, importBlock: ''),
    );
    return ExecuteResult.success(result);
  } catch (e, st) {
    return ExecuteResult.failure(e.toString(), stackTrace: st);
  }
}