| 197 | return JSValueRef(); |
| 198 | } |
| 199 | |
| 200 | JSValueRef V8JavaScriptContext::evaluate(const std::string& script, |
| 201 | const std::string_view& sourceFilename, |
| 202 | JSExceptionTracker& exceptionTracker) { |
| 203 | v8::HandleScope handleScope(_isolate); |
| 204 | v8::TryCatch catcher(_isolate); |
| 205 | |
| 206 | v8::Local<v8::Context> context = v8::Local<v8::Context>::New(_isolate, _context); |
| 207 | v8::TryCatch tryCatch(_isolate); |
| 208 | |
| 209 | v8::Context::Scope context_scope(context); |
| 210 | |
| 211 | v8::Local<v8::String> source; |
| 212 | if (!v8::String::NewFromUtf8( |
| 213 | _isolate, script.c_str(), v8::NewStringType::kNormal, static_cast<int32_t>(script.length())) |
| 214 | .ToLocal(&source)) { |
| 215 | exceptionTracker.onError("Failed to convert script into v8 backed utf8 string"); |
| 216 | return JSValueRef(); |
| 217 | } |
| 218 | |
| 219 | v8::Local<v8::Script> compiledSource; |
| 220 | if (!v8::Script::Compile(context, source).ToLocal(&compiledSource)) { |
| 221 | if (catcher.HasCaught()) { |
| 222 | auto excep = catcher.Exception(); |
| 223 | exceptionTracker.storeException(toRetainedJSValueRef(IndirectV8Persistent::make(_isolate, excep))); |
| 224 | } else { |
| 225 | exceptionTracker.onError("Failed to compile source"); |
| 226 | } |
| 227 | return JSValueRef(); |
| 228 | } |
| 229 | |
| 230 | v8::Local<v8::Value> result; |
| 231 | if (!compiledSource->Run(context).ToLocal(&result)) { |
| 232 | if (catcher.HasCaught()) { |
| 233 | auto excep = catcher.Exception(); |
| 234 | exceptionTracker.storeException(toRetainedJSValueRef(IndirectV8Persistent::make(_isolate, excep))); |
| 235 | } else { |
| 236 | exceptionTracker.onError("Failed to run compiled source"); |
| 237 | } |
| 238 | return JSValueRef(); |
| 239 | } |
| 240 | |
| 241 | return toRetainedJSValueRef(IndirectV8Persistent::make(_isolate, result)); |
| 242 | } |
| 243 | |
| 244 | JSValueRef V8JavaScriptContext::evaluatePreCompiled(const BytesView& /*bytes*/, |