initStdRequire method
void
initStdRequire()
Implementation
void initStdRequire() {
exec() {
final path = findVar('path');
final v = path?.valueAs<String>();
if (v == null) {
final type = v.runtimeType;
throw 'Expected string for "path" in `require(path)`. Was $type.';
}
Object? result;
final prevScope = scope;
try {
pushScope();
result = onRequireImpl?.call(v, this);
} catch (e) {
addError(e.toString());
} finally {
restoreScope(prevScope);
}
onRequireImplComplete?.call(v);
return result;
}
final token = Token.synthesized('require');
final defRequire = FuncExpr.named(
token,
body: [],
args: [DeclArg(Token.synthesized('path'))],
idParts: [RawExpr(token)],
);
defGlobal(LuaObject.func('require', defRequire, exec)).doc = LuaDoc(
category: catRuntime,
html: '''
The runtime resolves the lua script identified by <code>path</code>, executes,
and returns any values. This enables passing lua objects
between files.
<pre>
<code class="language-lua">local f = require('fibonacci.lua')
print(f(7)) -- prints 13
</code>
</pre>
''',
);
}