Beispiel #1
0
void ActiveScriptWrappable::traceActiveScriptWrappables(v8::Isolate* isolate, ScriptWrappableVisitor* visitor)
{
    V8PerIsolateData* isolateData = V8PerIsolateData::from(isolate);
    auto activeScriptWrappables = isolateData->activeScriptWrappables();
    if (!activeScriptWrappables) {
        return;
    }

    for (auto activeWrappable : *activeScriptWrappables) {
        if (!activeWrappable->hasPendingActivity())
            continue;

        ScriptWrappable* wrappable = activeWrappable->toScriptWrappable();
        wrappable->wrapperTypeInfo()->traceWrappers(visitor, wrappable);
    }
}
Beispiel #2
0
bool WorkerOrWorkletScriptController::initializeContextIfNeeded() {
  v8::HandleScope handleScope(m_isolate);

  if (isContextInitialized())
    return true;

  // Create a new v8::Context with the worker/worklet as the global object
  // (aka the inner global).
  ScriptWrappable* scriptWrappable = m_globalScope->getScriptWrappable();
  const WrapperTypeInfo* wrapperTypeInfo = scriptWrappable->wrapperTypeInfo();
  v8::Local<v8::FunctionTemplate> globalInterfaceTemplate =
      wrapperTypeInfo->domTemplate(m_isolate, *m_world);
  if (globalInterfaceTemplate.IsEmpty())
    return false;
  v8::Local<v8::ObjectTemplate> globalTemplate =
      globalInterfaceTemplate->InstanceTemplate();
  v8::Local<v8::Context> context;
  {
    // Initialize V8 extensions before creating the context.
    Vector<const char*> extensionNames;
    if (m_globalScope->isServiceWorkerGlobalScope() &&
        Platform::current()->allowScriptExtensionForServiceWorker(
            toWorkerGlobalScope(m_globalScope.get())->url())) {
      const V8Extensions& extensions = ScriptController::registeredExtensions();
      extensionNames.reserveInitialCapacity(extensions.size());
      for (const auto* extension : extensions)
        extensionNames.push_back(extension->name());
    }
    v8::ExtensionConfiguration extensionConfiguration(extensionNames.size(),
                                                      extensionNames.data());

    V8PerIsolateData::UseCounterDisabledScope useCounterDisabled(
        V8PerIsolateData::from(m_isolate));
    context =
        v8::Context::New(m_isolate, &extensionConfiguration, globalTemplate);
  }
  if (context.IsEmpty())
    return false;

  m_scriptState = ScriptState::create(context, m_world);

  ScriptState::Scope scope(m_scriptState.get());

  // The global proxy object.  Note this is not the global object.
  v8::Local<v8::Object> globalProxy = context->Global();
  V8DOMWrapper::setNativeInfo(m_isolate, globalProxy, wrapperTypeInfo,
                              scriptWrappable);

  // The global object, aka worker/worklet wrapper object.
  v8::Local<v8::Object> globalObject =
      globalProxy->GetPrototype().As<v8::Object>();
  globalObject = V8DOMWrapper::associateObjectWithWrapper(
      m_isolate, scriptWrappable, wrapperTypeInfo, globalObject);

  // All interfaces must be registered to V8PerContextData.
  // So we explicitly call constructorForType for the global object.
  V8PerContextData::from(context)->constructorForType(wrapperTypeInfo);

  // Name new context for debugging. For main thread worklet global scopes
  // this is done once the context is initialized.
  if (m_globalScope->isWorkerGlobalScope() ||
      m_globalScope->isThreadedWorkletGlobalScope()) {
    WorkerThreadDebugger* debugger = WorkerThreadDebugger::from(m_isolate);
    debugger->contextCreated(m_globalScope->thread(), context);
  }

  return true;
}