| 1400 | } |
| 1401 | |
| 1402 | func initializeContextGlobals(ctx *C.JSContext, code string, filename string) bool { |
| 1403 | if runtimeInitContextHook != nil { |
| 1404 | initRun := runtimeInitContextHook(ctx) |
| 1405 | if bool(C.JS_IsException(initRun)) { |
| 1406 | C.JS_FreeValue(ctx, initRun) |
| 1407 | return false |
| 1408 | } |
| 1409 | C.JS_FreeValue(ctx, initRun) |
| 1410 | return true |
| 1411 | } |
| 1412 | |
| 1413 | codeBuf := zeroTerminatedBytes(code) |
| 1414 | codePtr := (*C.char)(unsafe.Pointer(&codeBuf[0])) |
| 1415 | filenameBuf := zeroTerminatedBytes(filename) |
| 1416 | filenamePtr := (*C.char)(unsafe.Pointer(&filenameBuf[0])) |
| 1417 | |
| 1418 | var pinner goruntime.Pinner |
| 1419 | pinner.Pin(&codeBuf[0]) |
| 1420 | pinner.Pin(&filenameBuf[0]) |
| 1421 | defer pinner.Unpin() |
| 1422 | |
| 1423 | evalFlags := C.int(C.JS_EVAL_TYPE_MODULE) | C.int(C.JS_EVAL_FLAG_COMPILE_ONLY) |
| 1424 | initCompile := C.JS_Eval(ctx, codePtr, C.size_t(len(code)), filenamePtr, evalFlags) |
| 1425 | goruntime.KeepAlive(codeBuf) |
| 1426 | goruntime.KeepAlive(filenameBuf) |
| 1427 | if bool(C.JS_IsException(initCompile)) { |
| 1428 | C.JS_FreeValue(ctx, initCompile) |
| 1429 | return false |
| 1430 | } |
| 1431 | |
| 1432 | initEval := initCompile |
| 1433 | if runtimeEvalFunctionHook != nil { |
| 1434 | initEval = runtimeEvalFunctionHook(ctx, initCompile) |
| 1435 | } else { |
| 1436 | initEval = C.JS_EvalFunction(ctx, initCompile) |
| 1437 | } |
| 1438 | if bool(C.JS_IsException(initEval)) { |
| 1439 | C.JS_FreeValue(ctx, initEval) |
| 1440 | return false |
| 1441 | } |
| 1442 | |
| 1443 | initRun := C.AwaitValue(ctx, initEval) |
| 1444 | if bool(C.JS_IsException(initRun)) { |
| 1445 | C.JS_FreeValue(ctx, initRun) |
| 1446 | return false |
| 1447 | } |
| 1448 | |
| 1449 | C.JS_FreeValue(ctx, initRun) |
| 1450 | return true |
| 1451 | } |
| 1452 | |
| 1453 | func forceRuntimeNewContextFailureForTest(enable bool) func() { |
| 1454 | oldHook := runtimeNewContextHook |