continuedExecute method
dynamic
continuedExecute({})
Execute additional source code in the existing global context.
Unlike execute, this method does NOT reset the global environment. It reuses the existing module loader and environment from a previous execute call, allowing you to add more declarations and call functions while preserving all previously defined variables, functions, and classes.
Important: You must call execute at least once before calling continuedExecute to establish the execution context.
source The source code to execute.
name The name of the function to call. Defaults to 'main'.
positionalArgs The positional arguments to pass to the function.
namedArgs The named arguments to pass to the function.
library The URI of the named function source to load.
Example:
final d4rt = D4rt();
// Initial execution sets up context
d4rt.execute(source: 'void main() {}');
// Add more declarations without resetting
d4rt.continuedExecute(source: '''
int square(int x) => x * x;
void main() {}
''');
// The square function is now available
final result = d4rt.eval('square(5)'); // Returns 25
Implementation
dynamic continuedExecute({
String? source,
String name = 'main',
List<Object?>? positionalArgs,
Map<String, Object?>? namedArgs,
String? library,
}) {
if (!_hasExecutedOnce) {
throw RuntimeD4rtException(
'continuedExecute() requires an existing execution context. Call execute() first.',
);
}
Logger.debug(
"[D4rt.continuedExecute] Continuing execution in existing context. library: $library",
);
// Parse the source (reuses existing module loader for library resolution)
final compilationUnit = _parseSource(source: source, library: library);
// Reuse existing environment - don't register globals again
final executionEnvironment = _moduleLoader.globalEnvironment;
// Execute and return result
return _executeInEnvironment(
compilationUnit: compilationUnit,
executionEnvironment: executionEnvironment,
name: name,
positionalArgs: positionalArgs,
namedArgs: namedArgs,
library: library,
);
}