Index: extensions/renderer/module_system.cc |
diff --git a/extensions/renderer/module_system.cc b/extensions/renderer/module_system.cc |
index 05b51b2534dac7c628be080ca03ed8d3bfec6f00..03b1e542f2d95bc5e30fa3dab1fe8010adb27300 100644 |
--- a/extensions/renderer/module_system.cc |
+++ b/extensions/renderer/module_system.cc |
@@ -18,12 +18,15 @@ |
#include "extensions/renderer/console.h" |
#include "extensions/renderer/safe_builtins.h" |
#include "extensions/renderer/script_context.h" |
+#include "extensions/renderer/v8_helpers.h" |
#include "gin/modules/module_registry.h" |
#include "third_party/WebKit/public/web/WebFrame.h" |
#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
namespace extensions { |
+using namespace v8_helpers; |
+ |
namespace { |
const char* kModuleSystem = "module_system"; |
@@ -69,7 +72,7 @@ void Warn(v8::Isolate* isolate, const std::string& message) { |
class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
public: |
explicit DefaultExceptionHandler(ScriptContext* context) |
- : context_(context) {} |
+ : ModuleSystem::ExceptionHandler(context) {} |
// Fatally dumps the debug info from |try_catch| to the console. |
// Make sure this is never used for exceptions that originate in external |
@@ -77,14 +80,16 @@ class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
v8::HandleScope handle_scope(context_->isolate()); |
std::string stack_trace = "<stack trace unavailable>"; |
- if (!try_catch.StackTrace().IsEmpty()) { |
- v8::String::Utf8Value stack_value(try_catch.StackTrace()); |
+ v8::Local<v8::Value> v8_stack_trace; |
+ if (try_catch.StackTrace(context_->v8_context()).ToLocal(&v8_stack_trace)) { |
+ v8::String::Utf8Value stack_value(v8_stack_trace); |
if (*stack_value) |
stack_trace.assign(*stack_value, stack_value.length()); |
else |
stack_trace = "<could not convert stack trace to string>"; |
} |
- Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
+ Fatal(context_, |
+ CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
not at google - send to devlin
2015/06/16 20:52:24
Is this formatting change necessary? Either way, r
bashi
2015/06/17 01:40:45
Reverted.
|
} |
private: |
@@ -113,9 +118,11 @@ std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
error_message.assign(*error_message_v8, error_message_v8.length()); |
} |
+ auto maybe = message->GetLineNumber(context_->v8_context()); |
+ int line_number = maybe.IsJust() ? maybe.FromJust() : 0; |
return base::StringPrintf("%s:%d: %s", |
resource_name.c_str(), |
- message->GetLineNumber(), |
+ line_number, |
error_message.c_str()); |
} |
@@ -140,9 +147,9 @@ ModuleSystem::ModuleSystem(ScriptContext* context, SourceMap* source_map) |
v8::Local<v8::Object> global(context->v8_context()->Global()); |
v8::Isolate* isolate = context->isolate(); |
- global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField), |
+ global->SetHiddenValue(ToV8StringUnsafe(isolate, kModulesField), |
v8::Object::New(isolate)); |
- global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem), |
+ global->SetHiddenValue(ToV8StringUnsafe(isolate, kModuleSystem), |
v8::External::New(isolate, this)); |
gin::ModuleRegistry::From(context->v8_context())->AddObserver(this); |
@@ -161,10 +168,8 @@ void ModuleSystem::Invalidate() { |
{ |
v8::HandleScope scope(GetIsolate()); |
v8::Local<v8::Object> global = context()->v8_context()->Global(); |
- global->DeleteHiddenValue( |
- v8::String::NewFromUtf8(GetIsolate(), kModulesField)); |
- global->DeleteHiddenValue( |
- v8::String::NewFromUtf8(GetIsolate(), kModuleSystem)); |
+ global->DeleteHiddenValue(ToV8StringUnsafe(GetIsolate(), kModulesField)); |
+ global->DeleteHiddenValue(ToV8StringUnsafe(GetIsolate(), kModuleSystem)); |
} |
// Invalidate all active and clobbered NativeHandlers we own. |
@@ -192,21 +197,28 @@ void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
} |
v8::Local<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
+ if (module_name.size() >= v8::String::kMaxLength) |
+ return v8::Undefined(GetIsolate()); |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
return handle_scope.Escape(RequireForJsInner( |
- v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); |
+ ToV8StringUnsafe(GetIsolate(), module_name.c_str()))); |
} |
void ModuleSystem::RequireForJs( |
const v8::FunctionCallbackInfo<v8::Value>& args) { |
- v8::Local<v8::String> module_name = args[0]->ToString(args.GetIsolate()); |
+ if (!args[0]->IsString()) { |
+ NOTREACHED() << "require() called with a non-string argument"; |
+ return; |
+ } |
+ v8::Local<v8::String> module_name = args[0].As<v8::String>(); |
args.GetReturnValue().Set(RequireForJsInner(module_name)); |
} |
v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
v8::Local<v8::String> module_name) { |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
- v8::Context::Scope context_scope(context()->v8_context()); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Context::Scope context_scope(v8_context); |
v8::Local<v8::Object> global(context()->v8_context()->Global()); |
@@ -214,19 +226,20 @@ v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
// context keeps a reference to us, but our frame is destroyed (e.g. |
// background page keeps reference to chrome object in a closed popup). |
v8::Local<v8::Value> modules_value = global->GetHiddenValue( |
- v8::String::NewFromUtf8(GetIsolate(), kModulesField)); |
+ ToV8StringUnsafe(GetIsolate(), kModulesField)); |
if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
Warn(GetIsolate(), "Extension view no longer exists"); |
return v8::Undefined(GetIsolate()); |
} |
v8::Local<v8::Object> modules(v8::Local<v8::Object>::Cast(modules_value)); |
- v8::Local<v8::Value> exports(modules->Get(module_name)); |
- if (!exports->IsUndefined()) |
+ v8::Local<v8::Value> exports; |
+ if (!modules->Get(v8_context, module_name).ToLocal(&exports) || |
+ !exports->IsUndefined()) |
return handle_scope.Escape(exports); |
exports = LoadModule(*v8::String::Utf8Value(module_name)); |
- modules->Set(module_name, exports); |
+ SetProperty(v8_context, modules, module_name, exports); |
return handle_scope.Escape(exports); |
} |
@@ -260,13 +273,21 @@ v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
method_name); |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
- v8::Context::Scope context_scope(context()->v8_context()); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Context::Scope context_scope(v8_context); |
+ |
+ v8::Local<v8::String> v8_module_name; |
+ v8::Local<v8::String> v8_method_name; |
+ if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name) || |
+ !ToV8String(GetIsolate(), method_name.c_str(), &v8_method_name)) { |
+ return handle_scope.Escape( |
+ v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
not at google - send to devlin
2015/06/16 20:52:24
It actually looks like Handle and Local are the sa
bashi
2015/06/17 01:40:45
Done.
|
+ } |
v8::Local<v8::Value> module; |
{ |
NativesEnabledScope natives_enabled(this); |
- module = RequireForJsInner( |
- v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
+ module = RequireForJsInner(v8_module_name); |
} |
if (module.IsEmpty() || !module->IsObject()) { |
@@ -276,9 +297,10 @@ v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
} |
- v8::Local<v8::Value> value = v8::Local<v8::Object>::Cast(module)->Get( |
- v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); |
- if (value.IsEmpty() || !value->IsFunction()) { |
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module); |
+ v8::Local<v8::Value> value; |
+ if (!object->Get(v8_context, v8_method_name).ToLocal(&value) || |
+ !value->IsFunction()) { |
Fatal(context_, module_name + "." + method_name + " is not a function"); |
return handle_scope.Escape( |
v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
@@ -287,11 +309,13 @@ v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value); |
v8::Local<v8::Value> result; |
{ |
- v8::TryCatch try_catch; |
+ v8::TryCatch try_catch(GetIsolate()); |
try_catch.SetCaptureMessage(true); |
result = context_->CallFunction(func, argc, argv); |
- if (try_catch.HasCaught()) |
+ if (try_catch.HasCaught()) { |
HandleException(try_catch); |
+ result = v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())); |
+ } |
} |
return handle_scope.Escape(result); |
} |
@@ -311,22 +335,29 @@ void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
v8::HandleScope handle_scope(GetIsolate()); |
- RunString(v8::String::NewFromUtf8(GetIsolate(), code.c_str()), |
- v8::String::NewFromUtf8(GetIsolate(), name.c_str())); |
+ v8::Local<v8::String> v8_code; |
+ v8::Local<v8::String> v8_name; |
+ if (!ToV8String(GetIsolate(), code.c_str(), &v8_code) || |
+ !ToV8String(GetIsolate(), name.c_str(), &v8_name)) { |
+ Warn(GetIsolate(), "Too long code or name."); |
+ return; |
+ } |
+ RunString(v8_code, v8_name); |
} |
// static |
void ModuleSystem::NativeLazyFieldGetter( |
- v8::Local<v8::String> property, |
+ v8::Local<v8::Name> property, |
const v8::PropertyCallbackInfo<v8::Value>& info) { |
- LazyFieldGetterInner(property, info, &ModuleSystem::RequireNativeFromString); |
+ LazyFieldGetterInner(property.As<v8::String>(), info, |
+ &ModuleSystem::RequireNativeFromString); |
} |
// static |
void ModuleSystem::LazyFieldGetter( |
- v8::Local<v8::String> property, |
+ v8::Local<v8::Name> property, |
const v8::PropertyCallbackInfo<v8::Value>& info) { |
- LazyFieldGetterInner(property, info, &ModuleSystem::Require); |
+ LazyFieldGetterInner(property.As<v8::String>(), info, &ModuleSystem::Require); |
} |
// static |
@@ -342,7 +373,7 @@ void ModuleSystem::LazyFieldGetterInner( |
v8::Local<v8::Context> context = parameters->CreationContext(); |
v8::Local<v8::Object> global(context->Global()); |
v8::Local<v8::Value> module_system_value = global->GetHiddenValue( |
- v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); |
+ ToV8StringUnsafe(info.GetIsolate(), kModuleSystem)); |
if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
// ModuleSystem has been deleted. |
// TODO(kalman): See comment in header file. |
@@ -354,15 +385,21 @@ void ModuleSystem::LazyFieldGetterInner( |
ModuleSystem* module_system = static_cast<ModuleSystem*>( |
v8::Local<v8::External>::Cast(module_system_value)->Value()); |
- std::string name = *v8::String::Utf8Value(parameters->Get( |
- v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))); |
+ v8::Local<v8::String> v8_k_module_name = |
+ ToV8StringUnsafe(info.GetIsolate(), kModuleName); |
+ v8::Local<v8::Value> v8_module_name; |
+ if (!parameters->Get(context, v8_k_module_name).ToLocal(&v8_module_name)) { |
not at google - send to devlin
2015/06/16 20:52:24
What do you think about adding a "GetProperty" met
bashi
2015/06/17 01:40:45
Done.
|
+ Warn(info.GetIsolate(), "Cannot find module."); |
+ return; |
+ } |
+ std::string name = *v8::String::Utf8Value(v8_module_name); |
// Switch to our v8 context because we need functions created while running |
// the require()d module to belong to our context, not the current one. |
v8::Context::Scope context_scope(context); |
NativesEnabledScope natives_enabled_scope(module_system); |
- v8::TryCatch try_catch; |
+ v8::TryCatch try_catch(info.GetIsolate()); |
v8::Local<v8::Value> module_value = (module_system->*require_function)(name); |
if (try_catch.HasCaught()) { |
module_system->HandleException(try_catch); |
@@ -374,11 +411,20 @@ void ModuleSystem::LazyFieldGetterInner( |
} |
v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value); |
- v8::Local<v8::String> field = |
- parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) |
- ->ToString(info.GetIsolate()); |
+ v8::Local<v8::Value> field_value; |
+ if (!parameters->Get(context, |
+ ToV8StringUnsafe(info.GetIsolate(), kModuleField)) |
+ .ToLocal(&field_value)) { |
+ module_system->HandleException(try_catch); |
+ return; |
+ } |
+ v8::Local<v8::String> field; |
+ if (!field_value->ToString(context).ToLocal(&field)) { |
+ module_system->HandleException(try_catch); |
+ return; |
+ } |
- if (!module->Has(field)) { |
+ if (!CheckV8Call(module->Has(context, field))) { |
std::string field_str = *v8::String::Utf8Value(field); |
Fatal(module_system->context_, |
"Lazy require of " + name + "." + field_str + " did not set the " + |
@@ -386,8 +432,8 @@ void ModuleSystem::LazyFieldGetterInner( |
return; |
} |
- v8::Local<v8::Value> new_field = module->Get(field); |
- if (try_catch.HasCaught()) { |
+ v8::Local<v8::Value> new_field; |
+ if (!module->Get(context, field).ToLocal(&new_field)) { |
module_system->HandleException(try_catch); |
return; |
} |
@@ -401,8 +447,8 @@ void ModuleSystem::LazyFieldGetterInner( |
v8::Local<v8::Value> val = info.This(); |
if (val->IsObject()) { |
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(val); |
- object->Delete(property); |
- object->Set(property, new_field); |
+ object->Delete(context, property); |
+ SetProperty(context, object, property, new_field); |
} else { |
NOTREACHED(); |
} |
@@ -421,17 +467,21 @@ void ModuleSystem::SetLazyField(v8::Local<v8::Object> object, |
const std::string& field, |
const std::string& module_name, |
const std::string& module_field, |
- v8::AccessorGetterCallback getter) { |
+ v8::AccessorNameGetterCallback getter) { |
+ CHECK(field.size() < v8::String::kMaxLength); |
+ CHECK(module_name.size() < v8::String::kMaxLength); |
+ CHECK(module_field.size() < v8::String::kMaxLength); |
v8::HandleScope handle_scope(GetIsolate()); |
v8::Local<v8::Object> parameters = v8::Object::New(GetIsolate()); |
- parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName), |
- v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
- parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField), |
- v8::String::NewFromUtf8(GetIsolate(), module_field.c_str())); |
- object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()), |
- getter, |
- NULL, |
- parameters); |
+ v8::Local<v8::Context> context = context_->v8_context(); |
+ SetProperty(context, parameters, ToV8StringUnsafe(GetIsolate(), kModuleName), |
+ ToV8StringUnsafe(GetIsolate(), module_name.c_str())); |
+ SetProperty(context, parameters, ToV8StringUnsafe(GetIsolate(), kModuleField), |
+ ToV8StringUnsafe(GetIsolate(), module_field.c_str())); |
+ auto maybe = object->SetAccessor( |
+ context, ToV8StringUnsafe(GetIsolate(), field.c_str()), getter, NULL, |
+ parameters); |
+ CHECK(CheckV8Call(maybe)); |
} |
void ModuleSystem::SetNativeLazyField(v8::Local<v8::Object> object, |
@@ -448,27 +498,32 @@ void ModuleSystem::SetNativeLazyField(v8::Local<v8::Object> object, |
v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code, |
v8::Local<v8::String> name) { |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
- v8::Context::Scope context_scope(context()->v8_context()); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Context::Scope context_scope(v8_context); |
// Prepend extensions:: to |name| so that internal code can be differentiated |
// from external code in stack traces. This has no effect on behaviour. |
std::string internal_name = |
base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name)); |
+ if (internal_name.size() >= v8::String::kMaxLength) { |
+ NOTREACHED() << "internal_name is too long."; |
+ return v8::Undefined(GetIsolate()); |
+ } |
+ |
blink::WebScopedMicrotaskSuppression suppression; |
- v8::TryCatch try_catch; |
+ v8::TryCatch try_catch(GetIsolate()); |
try_catch.SetCaptureMessage(true); |
- v8::Local<v8::Script> script(v8::Script::Compile( |
- code, v8::String::NewFromUtf8(GetIsolate(), internal_name.c_str(), |
- v8::String::kNormalString, |
- internal_name.size()))); |
- if (try_catch.HasCaught()) { |
+ v8::ScriptOrigin origin( |
+ ToV8StringUnsafe(GetIsolate(), internal_name.c_str())); |
+ v8::Local<v8::Script> script; |
+ if (!v8::Script::Compile(v8_context, code, &origin).ToLocal(&script)) { |
HandleException(try_catch); |
return v8::Undefined(GetIsolate()); |
} |
- v8::Local<v8::Value> result = script->Run(); |
- if (try_catch.HasCaught()) { |
+ v8::Local<v8::Value> result; |
+ if (!script->Run(v8_context).ToLocal(&result)) { |
HandleException(try_catch); |
return v8::Undefined(GetIsolate()); |
} |
@@ -499,7 +554,7 @@ v8::Local<v8::Value> ModuleSystem::RequireNativeFromString( |
// we could crash. |
if (exception_handler_) { |
return GetIsolate()->ThrowException( |
- v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); |
+ ToV8StringUnsafe(GetIsolate(), "Natives disabled")); |
} |
Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); |
return v8::Undefined(GetIsolate()); |
@@ -507,7 +562,7 @@ v8::Local<v8::Value> ModuleSystem::RequireNativeFromString( |
if (overridden_native_handlers_.count(native_name) > 0u) { |
return RequireForJsInner( |
- v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); |
+ ToV8StringUnsafe(GetIsolate(), native_name.c_str())); |
} |
NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
@@ -523,16 +578,17 @@ void ModuleSystem::RequireAsync( |
const v8::FunctionCallbackInfo<v8::Value>& args) { |
CHECK_EQ(1, args.Length()); |
std::string module_name = *v8::String::Utf8Value(args[0]); |
+ v8::Local<v8::Context> v8_context = context_->v8_context(); |
v8::Local<v8::Promise::Resolver> resolver( |
- v8::Promise::Resolver::New(GetIsolate())); |
+ v8::Promise::Resolver::New(v8_context).ToLocalChecked()); |
args.GetReturnValue().Set(resolver->GetPromise()); |
scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver( |
new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver)); |
gin::ModuleRegistry* module_registry = |
- gin::ModuleRegistry::From(context_->v8_context()); |
+ gin::ModuleRegistry::From(v8_context); |
if (!module_registry) { |
Warn(GetIsolate(), "Extension view no longer exists"); |
- resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( |
+ resolver->Reject(v8_context, v8::Exception::Error(ToV8StringUnsafe( |
GetIsolate(), "Extension view no longer exists"))); |
return; |
} |
@@ -547,13 +603,13 @@ void ModuleSystem::RequireAsync( |
v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) { |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
// Keep in order with the arguments in RequireForJsInner. |
- v8::Local<v8::String> left = v8::String::NewFromUtf8( |
+ v8::Local<v8::String> left = ToV8StringUnsafe( |
GetIsolate(), |
"(function(define, require, requireNative, requireAsync, exports, " |
"console, privates," |
"$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" |
"'use strict';"); |
- v8::Local<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); |
+ v8::Local<v8::String> right = ToV8StringUnsafe(GetIsolate(), "\n})"); |
return handle_scope.Escape(v8::Local<v8::String>( |
v8::String::Concat(left, v8::String::Concat(source, right)))); |
} |
@@ -562,7 +618,7 @@ void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
CHECK_EQ(1, args.Length()); |
if (!args[0]->IsObject() || args[0]->IsNull()) { |
GetIsolate()->ThrowException( |
- v8::Exception::TypeError(v8::String::NewFromUtf8(GetIsolate(), |
+ v8::Exception::TypeError(ToV8StringUnsafe(GetIsolate(), |
args[0]->IsUndefined() |
? "Method called without a valid receiver (this). " |
"Did you forget to call .bind()?" |
@@ -571,13 +627,13 @@ void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
} |
v8::Local<v8::Object> obj = args[0].As<v8::Object>(); |
v8::Local<v8::String> privates_key = |
- v8::String::NewFromUtf8(GetIsolate(), "privates"); |
+ ToV8StringUnsafe(GetIsolate(), "privates"); |
v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key); |
if (privates.IsEmpty()) { |
privates = v8::Object::New(args.GetIsolate()); |
if (privates.IsEmpty()) { |
GetIsolate()->ThrowException( |
- v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); |
+ ToV8StringUnsafe(GetIsolate(), "Failed to create privates")); |
return; |
} |
obj->SetHiddenValue(privates_key, privates); |
@@ -587,7 +643,8 @@ void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
v8::EscapableHandleScope handle_scope(GetIsolate()); |
- v8::Context::Scope context_scope(context()->v8_context()); |
+ v8::Local<v8::Context> v8_context = context()->v8_context(); |
+ v8::Context::Scope context_scope(v8_context); |
v8::Local<v8::Value> source(GetSource(module_name)); |
if (source.IsEmpty() || source->IsUndefined()) { |
@@ -596,10 +653,14 @@ v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
} |
v8::Local<v8::String> wrapped_source( |
WrapSource(v8::Local<v8::String>::Cast(source))); |
+ v8::Local<v8::String> v8_module_name; |
+ if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name)) { |
+ NOTREACHED() << "module_name is too long"; |
+ return v8::Undefined(GetIsolate()); |
+ } |
// Modules are wrapped in (function(){...}) so they always return functions. |
v8::Local<v8::Value> func_as_value = |
- RunString(wrapped_source, |
- v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
+ RunString(wrapped_source, v8_module_name); |
if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
Fatal(context_, "Bad source for require(" + module_name + ")"); |
return v8::Undefined(GetIsolate()); |
@@ -612,24 +673,29 @@ v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); |
v8::Local<v8::Object> natives(NewInstance()); |
- CHECK(!natives.IsEmpty()); // this can happen if v8 has issues |
+ CHECK(!natives.IsEmpty()); // this can fail if v8 has issues |
// These must match the argument order in WrapSource. |
v8::Local<v8::Value> args[] = { |
// AMD. |
- define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")), |
+ UnsafeGet(v8_context, define_object, |
+ ToV8StringUnsafe(GetIsolate(), "define")), |
// CommonJS. |
- natives->Get(v8::String::NewFromUtf8(GetIsolate(), "require", |
- v8::String::kInternalizedString)), |
- natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireNative", |
- v8::String::kInternalizedString)), |
- natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireAsync", |
- v8::String::kInternalizedString)), |
+ UnsafeGet(v8_context, natives, |
+ ToV8StringUnsafe(GetIsolate(), "require", |
+ v8::NewStringType::kInternalized)), |
+ UnsafeGet(v8_context, natives, |
+ ToV8StringUnsafe(GetIsolate(), "requireNative", |
+ v8::NewStringType::kInternalized)), |
+ UnsafeGet(v8_context, natives, |
+ ToV8StringUnsafe(GetIsolate(), "requireAsync", |
+ v8::NewStringType::kInternalized)), |
exports, |
// Libraries that we magically expose to every module. |
console::AsV8Object(GetIsolate()), |
- natives->Get(v8::String::NewFromUtf8(GetIsolate(), "privates", |
- v8::String::kInternalizedString)), |
+ UnsafeGet(v8_context, natives, |
+ ToV8StringUnsafe(GetIsolate(), "privates", |
+ v8::NewStringType::kInternalized)), |
// Each safe builtin. Keep in order with the arguments in WrapSource. |
context_->safe_builtins()->GetArray(), |
context_->safe_builtins()->GetFunction(), |
@@ -640,7 +706,7 @@ v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
context_->safe_builtins()->GetError(), |
}; |
{ |
- v8::TryCatch try_catch; |
+ v8::TryCatch try_catch(GetIsolate()); |
try_catch.SetCaptureMessage(true); |
context_->CallFunction(func, arraysize(args), args); |
if (try_catch.HasCaught()) { |
@@ -680,7 +746,7 @@ void ModuleSystem::OnModuleLoaded( |
v8::HandleScope handle_scope(GetIsolate()); |
v8::Local<v8::Promise::Resolver> resolver_local( |
v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); |
- resolver_local->Resolve(value); |
+ resolver_local->Resolve(context()->v8_context(), value); |
} |
void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { |