OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "bindings/core/v8/ScriptModule.h" | |
6 | |
7 #include "bindings/core/v8/ScriptModuleResolver.h" | |
8 #include "bindings/core/v8/V8Binding.h" | |
9 #include "bindings/core/v8/V8PerContextData.h" | |
10 | |
11 namespace blink { | |
12 | |
13 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) | |
14 : m_module(SharedPersistent<v8::Module>::create(module, isolate)), | |
15 m_identityHash(static_cast<unsigned>(module->GetIdentityHash())) {} | |
16 | |
17 ScriptModule::~ScriptModule() {} | |
18 | |
19 ScriptModule ScriptModule::compile(v8::Isolate* isolate, | |
20 const String& source, | |
21 const String& fileName) { | |
22 // TODO(adamk): Pass more info into ScriptOrigin. | |
23 v8::ScriptOrigin origin(v8String(isolate, fileName)); | |
24 v8::ScriptCompiler::Source scriptSource(v8String(isolate, source), origin); | |
25 v8::TryCatch tryCatch(isolate); | |
26 tryCatch.SetVerbose(true); | |
27 v8::Local<v8::Module> module; | |
28 if (!v8Call(v8::ScriptCompiler::CompileModule(isolate, &scriptSource), module, | |
haraken
2017/01/06 05:47:58
Nit: I want to create a helper function in V8Scrip
kouhei (in TOK)
2017/01/11 01:41:57
Acknowledged. TODO for now.
| |
29 tryCatch)) { | |
30 // TODO(adamk): Signal failure somehow. | |
31 return ScriptModule(isolate, module); | |
32 } | |
33 return ScriptModule(isolate, module); | |
34 } | |
35 | |
36 v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( | |
37 v8::Local<v8::Context> context, | |
38 v8::Local<v8::String> specifier, | |
39 v8::Local<v8::Module> referrer) { | |
40 v8::Isolate* isolate = context->GetIsolate(); | |
41 ScriptModuleResolver* resolver = | |
42 V8PerContextData::from(context)->moduleResolver(); | |
43 DCHECK(resolver); | |
44 ScriptModule referrerRecord(isolate, referrer); | |
45 // TODO(kouhei): Construct ScriptModuleIdentifier w/o creating extra | |
46 // Persistent. | |
47 ScriptModule resolved = resolver->resolve( | |
48 toCoreStringWithNullCheck(specifier), referrerRecord.identifier()); | |
49 if (resolved.isNull()) | |
50 return v8::MaybeLocal<v8::Module>(); | |
51 | |
52 return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); | |
53 } | |
54 | |
55 bool ScriptModule::instantiate(ScriptState* scriptState) { | |
56 DCHECK(!isNull()); | |
57 v8::Local<v8::Context> context = scriptState->context(); | |
58 // TODO(adamk): pass in a real callback. | |
59 return m_module->newLocal(scriptState->isolate()) | |
60 ->Instantiate(context, &resolveModuleCallback); | |
61 } | |
62 | |
63 void ScriptModule::evaluate(ScriptState* scriptState) { | |
64 v8::TryCatch tryCatch(scriptState->isolate()); | |
65 tryCatch.SetVerbose(true); | |
66 v8::Local<v8::Value> result; | |
67 if (!v8Call(m_module->newLocal(scriptState->isolate()) | |
68 ->Evaluate(scriptState->context()), | |
haraken
2017/01/06 05:47:58
Ditto.
kouhei (in TOK)
2017/01/11 01:41:57
Acknowledged.
| |
69 result, tryCatch)) { | |
70 // TODO(adamk): report error | |
71 } | |
72 } | |
73 | |
74 Vector<String> ScriptModule::moduleRequests(ScriptState* scriptState) { | |
75 if (isNull()) | |
76 return Vector<String>(); | |
77 | |
78 v8::Local<v8::Module> module = m_module->newLocal(scriptState->isolate()); | |
79 | |
80 Vector<String> ret; | |
81 | |
82 int length = module->GetModuleRequestsLength(); | |
83 ret.reserveInitialCapacity(length); | |
84 for (int i = 0; i < length; ++i) { | |
85 v8::Local<v8::String> v8Name = module->GetModuleRequest(i); | |
86 String name = toCoreStringWithNullCheck(v8Name); | |
87 ret.push_back(name); | |
88 } | |
89 return ret; | |
90 } | |
91 | |
92 ScriptModuleIdentifier ScriptModule::identifier() { | |
93 if (isNull()) | |
94 return ScriptModuleIdentifier(); | |
95 | |
96 return ScriptModuleIdentifier(m_module, m_identityHash); | |
97 } | |
98 | |
99 } // namespace blink | |
OLD | NEW |