MCPcopy Create free account
hub / github.com/Kudo/react-native-v8 / ExecuteScript

Method ExecuteScript

src/v8runtime/V8Runtime.cpp:207–257  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

205}
206
207jsi::Value V8Runtime::ExecuteScript(
208 v8::Isolate *isolate,
209 const v8::Local<v8::String> &script,
210 const std::string &sourceURL) {
211 v8::HandleScope scopedHandle(isolate);
212 v8::TryCatch tryCatch(isolate);
213
214 v8::MaybeLocal<v8::String> sourceURLValue = v8::String::NewFromUtf8(
215 isolate,
216 sourceURL.c_str(),
217 v8::NewStringType::kNormal,
218 static_cast<int>(sourceURL.length()));
219 v8::ScriptOrigin origin(isolate, sourceURLValue.ToLocalChecked());
220
221 v8::Local<v8::Context> context(isolate->GetCurrentContext());
222
223 auto codecache = LoadCodeCacheIfNeeded(sourceURL);
224 v8::ScriptCompiler::CachedData *cachedData = codecache.release();
225
226 std::unique_ptr<v8::ScriptCompiler::Source> source =
227 UseFakeSourceIfNeeded(origin, cachedData);
228 if (!source) {
229 source = std::make_unique<v8::ScriptCompiler::Source>(
230 script, origin, cachedData);
231 }
232
233 v8::Local<v8::Script> compiledScript;
234 if (!v8::ScriptCompiler::Compile(
235 context,
236 source.release(),
237 cachedData ? v8::ScriptCompiler::kConsumeCodeCache
238 : v8::ScriptCompiler::kNoCompileOptions)
239 .ToLocal(&compiledScript)) {
240 ReportException(isolate, &tryCatch);
241 return {};
242 }
243
244 if (cachedData && cachedData->rejected) {
245 LOG(INFO) << "[rnv8] cache miss: " << sourceURL;
246 }
247 SaveCodeCacheIfNeeded(compiledScript, sourceURL, cachedData);
248
249 v8::Local<v8::Value> result;
250 if (!compiledScript->Run(context).ToLocal(&result)) {
251 assert(tryCatch.HasCaught());
252 ReportException(isolate, &tryCatch);
253 return {};
254 }
255
256 return JSIV8ValueConverter::ToJSIValue(isolate, result);
257}
258
259void V8Runtime::ReportException(v8::Isolate *isolate, v8::TryCatch *tryCatch)
260 const {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected