GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ios/NativeScript/runtime/BuiltinLoader.cpp at main · NativeScript/ios Expand file tree/ Copy pathBuiltinLoader.cppMore file actions 251 lines (214 loc) · 8.98 KB / Copy pathBuiltinLoader.cppFile metadata and controls251 lines (214 loc) · 8.98 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 #include "BuiltinLoader.h" #include <mutex> #include <vector> #include "Caches.h" #include "Helpers.h" #include "NsBuiltinModules.h" using namespace v8; namespace tns { namespace { // Process-wide bytecode cache shared across isolates (main + workers). std::mutex builtinCacheMutex; std::vector<uint8_t> builtinCache[static_cast<unsigned>(BuiltinId::kCount)]; // Every builtin is compiled as a function body receiving these fixed // parameters, mirroring Node's module wrapper: a file exports through // `module.exports`/`exports`, reaches sibling builtin modules through // `require`, natives arrive as properties of the `binding` bag (Node's // internalBinding idiom) and intrinsics as properties of `primordials`; each // file destructures what it needs. constexpr const char* kExportsParamName = "exports"; constexpr const char* kRequireParamName = "require"; constexpr const char* kModuleParamName = "module"; constexpr const char* kBindingParamName = "binding"; constexpr const char* kPrimordialsParamName = "primordials"; constexpr int kParamCount = 5; // `module.exports` of every builtin that has run in this isolate, indexed by // id. Per isolate because a builtin is a singleton per realm, so workers run // their own copy of a file and export their own objects. struct BuiltinExportsState { Persistent<Object> exports[static_cast<unsigned>(BuiltinId::kCount)]; }; // The `require` every builtin receives: builtin specifiers only — including // the internal tier app code can never name — so a builtin can never reach // application code or the filesystem. void BuiltinRequireCallback(const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); if (info.Length() < 1 || !info[0]->IsString()) { isolate->ThrowException(Exception::TypeError( tns::ToV8String(isolate, "require() expects a specifier string"))); return; } Local<Context> context = isolate->GetCurrentContext(); std::string specifier = tns::ToString(isolate, info[0].As<v8::String>()); Local<Object> exports; if (NsBuiltinModules::GetExports(context, specifier).ToLocal(&exports)) { info.GetReturnValue().Set(exports); } else if (!NsBuiltinModules::IsRegistered(specifier, /* includeInternal */ true)) { isolate->ThrowException(Exception::Error(tns::ToV8String( isolate, NsBuiltinModules::NotFoundMessage(specifier)))); } } MaybeLocal<v8::Function> GetBuiltinRequire(Local<Context> context) { Isolate* isolate = v8::Isolate::GetCurrent(); std::shared_ptr<Caches> cache = Caches::Get(isolate); if (cache->BuiltinRequire != nullptr) { return cache->BuiltinRequire->Get(isolate); } Local<v8::Function> require; if (!v8::Function::New(context, BuiltinRequireCallback, Local<Value>(), 1, ConstructorBehavior::kThrow) .ToLocal(&require)) { return MaybeLocal<v8::Function>(); } cache->BuiltinRequire = std::make_unique<Persistent<v8::Function>>(isolate, require); return require; } MaybeLocal<v8::Function> CompileBuiltin(Local<Context> context, BuiltinId id) { Isolate* isolate = v8::Isolate::GetCurrent(); const BuiltinSource& builtin = GetBuiltinSource(id); const unsigned index = static_cast<unsigned>(id); // Copy the blob out so the shared slot can be refreshed concurrently while // this compile still reads from the copy. std::vector<uint8_t> blob; { std::lock_guard<std::mutex> lock(builtinCacheMutex); blob = builtinCache[index]; } ScriptOrigin origin(tns::ToV8String(isolate, builtin.name), 0, // line offset 0, // column offset false, // shared_cross_origin -1, // script_id Local<Value>(), false, // is_opaque false, // is_wasm false // is_module ); Local<v8::String> sourceText = tns::ToV8String( isolate, builtin.source, static_cast<int>(builtin.length)); Local<v8::String> params[] = { tns::ToV8String(isolate, kExportsParamName), tns::ToV8String(isolate, kRequireParamName), tns::ToV8String(isolate, kModuleParamName), tns::ToV8String(isolate, kBindingParamName), tns::ToV8String(isolate, kPrimordialsParamName)}; Local<v8::Function> fn; if (!blob.empty()) { // The Source owns and deletes the CachedData object; BufferNotOwned keeps // the underlying bytes (our copy) out of its hands. auto* cachedData = new ScriptCompiler::CachedData( blob.data(), static_cast<int>(blob.size()), ScriptCompiler::CachedData::BufferNotOwned); ScriptCompiler::Source source(sourceText, origin, cachedData); if (ScriptCompiler::CompileFunction(context, &source, kParamCount, params, 0, nullptr, ScriptCompiler::kConsumeCodeCache) .ToLocal(&fn) && !cachedData->rejected) { return fn; } // Rejected cache (e.g. produced under different flags): fall through and // recompile eagerly so the refreshed blob covers inner functions again. } ScriptCompiler::Source source(sourceText, origin); if (!ScriptCompiler::CompileFunction(context, &source, kParamCount, params, 0, nullptr, ScriptCompiler::kEagerCompile) .ToLocal(&fn)) { return MaybeLocal<v8::Function>(); } std::unique_ptr<ScriptCompiler::CachedData> produced( ScriptCompiler::CreateCodeCacheForFunction(fn)); if (produced != nullptr && produced->data != nullptr && produced->length > 0) { std::lock_guard<std::mutex> lock(builtinCacheMutex); builtinCache[index].assign(produced->data, produced->data + produced->length); } return fn; } MaybeLocal<Value> CallBuiltin(Local<Context> context, BuiltinId id, Local<Value> binding, Local<Value> primordials) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<v8::Function> fn; if (!CompileBuiltin(context, id).ToLocal(&fn)) { return MaybeLocal<Value>(); } Local<v8::Function> require; if (!GetBuiltinRequire(context).ToLocal(&require)) { return MaybeLocal<Value>(); } Local<Object> exportsObj = Object::New(isolate); Local<Object> moduleObj = Object::New(isolate); Local<v8::String> exportsKey = tns::ToV8String(isolate, kExportsParamName); if (!moduleObj->Set(context, exportsKey, exportsObj).FromMaybe(false)) { return MaybeLocal<Value>(); } Local<Value> args[] = { exportsObj, require, moduleObj, binding.IsEmpty() ? v8::Undefined(isolate).As<Value>() : binding, primordials}; if (fn->Call(context, v8::Undefined(isolate), kParamCount, args).IsEmpty()) { return MaybeLocal<Value>(); } return moduleObj->Get(context, exportsKey); } // Snapshot of the intrinsics, taken the first time any builtin runs in this // isolate — during runtime init, before user code can replace a global. // Builtins compiled later in the isolate's life get the same pristine // snapshot. MaybeLocal<Object> GetPrimordials(Local<Context> context) { Isolate* isolate = v8::Isolate::GetCurrent(); std::shared_ptr<Caches> cache = Caches::Get(isolate); if (cache->Primordials != nullptr) { return cache->Primordials->Get(isolate); } Local<Value> result; if (!CallBuiltin(context, BuiltinId::kPrimordials, Local<Value>(), v8::Undefined(isolate)) .ToLocal(&result) || !result->IsObject()) { return MaybeLocal<Object>(); } Local<Object> primordials = result.As<Object>(); cache->Primordials = std::make_unique<Persistent<Object>>(isolate, primordials); return primordials; } } // namespace MaybeLocal<Value> BuiltinLoader::RunBuiltin(Local<Context> context, BuiltinId id, Local<Value> binding) { Local<Object> primordials; if (!GetPrimordials(context).ToLocal(&primordials)) { return MaybeLocal<Value>(); } return CallBuiltin(context, id, binding, primordials); } MaybeLocal<Object> BuiltinLoader::GetExports(Local<Context> context, BuiltinId id, BindingFactory bindingFactory) { Isolate* isolate = v8::Isolate::GetCurrent(); auto* state = Caches::StateFor<BuiltinExportsState>(isolate); if (state == nullptr) { return MaybeLocal<Object>(); } const unsigned index = static_cast<unsigned>(id); if (!state->exports[index].IsEmpty()) { return state->exports[index].Get(isolate); } Local<Object> binding; if (bindingFactory != nullptr && !bindingFactory(context).ToLocal(&binding)) { return MaybeLocal<Object>(); } Local<Value> result; if (!RunBuiltin(context, id, binding).ToLocal(&result) || !result->IsObject()) { return MaybeLocal<Object>(); } Local<Object> exports = result.As<Object>(); state->exports[index].Reset(isolate, exports); return exports; } } // namespace tns |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |