evaluate method
Implementation
@override
JsEvalResult evaluate(String js, {String? sourceUrl}) {
Pointer<Utf8> scriptCString = js.toNativeUtf8();
Pointer<Utf8>? sourceUrlCString = sourceUrl?.toNativeUtf8();
JSValuePointer exception = JSValuePointer();
var jsValueRef = jSEvaluateScript(
_globalContext,
jSStringCreateWithUTF8CString(scriptCString),
nullptr,
sourceUrlCString != null
? jSStringCreateWithUTF8CString(sourceUrlCString)
: nullptr,
1,
exception.pointer);
calloc.free(scriptCString);
if (sourceUrlCString != null) {
calloc.free(sourceUrlCString);
}
String result;
JSValue exceptionValue = exception.getValue(context);
bool isPromise = false;
if (exceptionValue.isObject) {
result =
'ERROR: ${exceptionValue.toObject().getProperty("message").string} \n at ${exceptionValue.toObject().getProperty("stack").string}';
} else {
result = _getJsValue(jsValueRef);
JSValue resultValue = JSValuePointer(jsValueRef).getValue(context);
isPromise = resultValue.isObject &&
resultValue.toObject().getProperty('then').isObject &&
resultValue.toObject().getProperty('catch').isObject;
}
return JsEvalResult(
result,
exceptionValue.isObject ? exceptionValue.toObject().pointer : jsValueRef,
isError: result.startsWith('ERROR:'),
isPromise: isPromise,
);
}