|
| 1 | +#include "BuiltinLoader.h" |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "ArgConverter.h" |
| 7 | + |
| 8 | +using namespace v8; |
| 9 | + |
| 10 | +namespace tns { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +/* |
| 15 | + * Process-wide bytecode cache shared across isolates. Worker runtimes |
| 16 | + * initialize on their own threads, so every access is under the mutex. |
| 17 | + */ |
| 18 | +std::mutex builtinCacheMutex; |
| 19 | +std::vector<uint8_t> builtinCache[static_cast<unsigned>(BuiltinId::kCount)]; |
| 20 | + |
| 21 | +/* |
| 22 | + * Every builtin is compiled as a function body receiving these fixed |
| 23 | + * parameters, mirroring Node's module wrapper: a file exports through |
| 24 | + * `module.exports`/`exports`, and natives arrive as properties of the |
| 25 | + * `binding` bag (Node's internalBinding idiom) for each file to destructure. |
| 26 | + */ |
| 27 | +constexpr const char* kExportsParamName = "exports"; |
| 28 | +constexpr const char* kModuleParamName = "module"; |
| 29 | +constexpr const char* kBindingParamName = "binding"; |
| 30 | +constexpr size_t kParamCount = 3; |
| 31 | + |
| 32 | +MaybeLocal<v8::Function> CompileBuiltin(Local<Context> context, BuiltinId id) { |
| 33 | + Isolate* isolate = v8::Isolate::GetCurrent(); |
| 34 | + const BuiltinSource& builtin = GetBuiltinSource(id); |
| 35 | + const unsigned index = static_cast<unsigned>(id); |
| 36 | + |
| 37 | + // Copy the blob out so the shared slot can be refreshed concurrently while |
| 38 | + // this compile still reads from the copy. |
| 39 | + std::vector<uint8_t> blob; |
| 40 | + { |
| 41 | + std::lock_guard<std::mutex> lock(builtinCacheMutex); |
| 42 | + blob = builtinCache[index]; |
| 43 | + } |
| 44 | + |
| 45 | + ScriptOrigin origin(ArgConverter::ConvertToV8String(isolate, builtin.name)); |
| 46 | + Local<v8::String> sourceText = ArgConverter::ConvertToV8String( |
| 47 | + isolate, builtin.source, static_cast<int>(builtin.length)); |
| 48 | + Local<v8::String> params[] = { |
| 49 | + ArgConverter::ConvertToV8String(isolate, kExportsParamName), |
| 50 | + ArgConverter::ConvertToV8String(isolate, kModuleParamName), |
| 51 | + ArgConverter::ConvertToV8String(isolate, kBindingParamName)}; |
| 52 | + |
| 53 | + Local<v8::Function> fn; |
| 54 | + if (!blob.empty()) { |
| 55 | + // The Source owns and deletes the CachedData object; BufferNotOwned |
| 56 | + // keeps the underlying bytes (our copy) out of its hands. |
| 57 | + auto* cachedData = new ScriptCompiler::CachedData( |
| 58 | + blob.data(), static_cast<int>(blob.size()), |
| 59 | + ScriptCompiler::CachedData::BufferNotOwned); |
| 60 | + ScriptCompiler::Source source(sourceText, origin, cachedData); |
| 61 | + if (ScriptCompiler::CompileFunction(context, &source, kParamCount, params, 0, nullptr, |
| 62 | + ScriptCompiler::kConsumeCodeCache) |
| 63 | + .ToLocal(&fn) && |
| 64 | + !cachedData->rejected) { |
| 65 | + return fn; |
| 66 | + } |
| 67 | + // Rejected cache (e.g. produced under different flags): fall through |
| 68 | + // and recompile eagerly so the refreshed blob covers inner functions |
| 69 | + // again. |
| 70 | + } |
| 71 | + |
| 72 | + ScriptCompiler::Source source(sourceText, origin); |
| 73 | + if (!ScriptCompiler::CompileFunction(context, &source, kParamCount, params, 0, nullptr, |
| 74 | + ScriptCompiler::kEagerCompile) |
| 75 | + .ToLocal(&fn)) { |
| 76 | + return MaybeLocal<v8::Function>(); |
| 77 | + } |
| 78 | + |
| 79 | + std::unique_ptr<ScriptCompiler::CachedData> produced( |
| 80 | + ScriptCompiler::CreateCodeCacheForFunction(fn)); |
| 81 | + if (produced != nullptr && produced->data != nullptr && produced->length > 0) { |
| 82 | + std::lock_guard<std::mutex> lock(builtinCacheMutex); |
| 83 | + builtinCache[index].assign(produced->data, produced->data + produced->length); |
| 84 | + } |
| 85 | + |
| 86 | + return fn; |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace |
| 90 | + |
| 91 | +MaybeLocal<Value> BuiltinLoader::RunBuiltin(Local<Context> context, BuiltinId id, |
| 92 | + Local<Value> binding) { |
| 93 | + Isolate* isolate = v8::Isolate::GetCurrent(); |
| 94 | + |
| 95 | + Local<v8::Function> fn; |
| 96 | + if (!CompileBuiltin(context, id).ToLocal(&fn)) { |
| 97 | + return MaybeLocal<Value>(); |
| 98 | + } |
| 99 | + |
| 100 | + Local<Object> exportsObj = Object::New(isolate); |
| 101 | + Local<Object> moduleObj = Object::New(isolate); |
| 102 | + Local<v8::String> exportsKey = ArgConverter::ConvertToV8String(isolate, kExportsParamName); |
| 103 | + if (!moduleObj->Set(context, exportsKey, exportsObj).FromMaybe(false)) { |
| 104 | + return MaybeLocal<Value>(); |
| 105 | + } |
| 106 | + |
| 107 | + Local<Value> args[] = {exportsObj, moduleObj, |
| 108 | + binding.IsEmpty() ? Undefined(isolate).As<Value>() : binding}; |
| 109 | + if (fn->Call(context, Undefined(isolate), static_cast<int>(kParamCount), args).IsEmpty()) { |
| 110 | + return MaybeLocal<Value>(); |
| 111 | + } |
| 112 | + |
| 113 | + return moduleObj->Get(context, exportsKey); |
| 114 | +} |
| 115 | + |
| 116 | +} // namespace tns |
0 commit comments