evaluateScripts function
Implementation
Future<bool> evaluateScripts(int contextId, String code, {String? url, int line = 0}) async {
if (MercuryController.getControllerOfJSContextId(contextId) == null) {
return false;
}
// Assign `vm://$id` for no url (anonymous scripts).
if (url == null) {
url = 'vm://$_anonymousScriptEvaluationId';
_anonymousScriptEvaluationId++;
}
QuickJSByteCodeCacheObject cacheObject = await QuickJSByteCodeCache.getCacheObject(code);
if (QuickJSByteCodeCacheObject.cacheMode == ByteCodeCacheMode.DEFAULT && cacheObject.valid && cacheObject.bytes != null) {
bool result = evaluateQuickjsByteCode(contextId, cacheObject.bytes!);
// If the bytecode evaluate failed, remove the cached file and fallback to raw javascript mode.
if (!result) {
await cacheObject.remove();
}
return result;
} else {
Pointer<NativeString> nativeString = stringToNativeString(code);
Pointer<Utf8> _url = url.toNativeUtf8();
try {
assert(_allocatedMercuryIsolates.containsKey(contextId));
int result;
if (QuickJSByteCodeCache.isCodeNeedCache(code)) {
// Export the bytecode from scripts
Pointer<Pointer<Uint8>> bytecodes = malloc.allocate(sizeOf<Pointer<Uint8>>());
Pointer<Uint64> bytecodeLen = malloc.allocate(sizeOf<Uint64>());
result = _evaluateScripts(_allocatedMercuryIsolates[contextId]!, nativeString, bytecodes, bytecodeLen, _url, line);
Uint8List bytes = bytecodes.value.asTypedList(bytecodeLen.value);
// Save to disk cache
QuickJSByteCodeCache.putObject(code, bytes);
} else {
result = _evaluateScripts(_allocatedMercuryIsolates[contextId]!, nativeString, nullptr, nullptr, _url, line);
}
return result == 1;
} catch (e, stack) {
print('$e\n$stack');
}
freeNativeString(nativeString);
malloc.free(_url);
}
return false;
}