OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/module_system.h" | 5 #include "extensions/renderer/module_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 v8::String::Utf8Value stack_value(v8_stack_trace); | 89 v8::String::Utf8Value stack_value(v8_stack_trace); |
90 if (*stack_value) | 90 if (*stack_value) |
91 stack_trace.assign(*stack_value, stack_value.length()); | 91 stack_trace.assign(*stack_value, stack_value.length()); |
92 else | 92 else |
93 stack_trace = "<could not convert stack trace to string>"; | 93 stack_trace = "<could not convert stack trace to string>"; |
94 } | 94 } |
95 Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); | 95 Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
96 } | 96 } |
97 }; | 97 }; |
98 | 98 |
99 // Sets a property on the "exports" object for bindings. Called by JS with | |
100 // exports.$set(<key>, <value>). | |
101 void SetExportsProperty( | |
102 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
103 v8::Local<v8::Object> obj = args.This(); | |
104 DCHECK_EQ(2, args.Length()); | |
105 DCHECK(args[0]->IsString()); | |
106 v8::Maybe<bool> result = | |
107 obj->DefineOwnProperty(args.GetIsolate()->GetCallingContext(), | |
jochen (gone - plz use gerrit)
2015/10/28 14:22:18
I know this is copy/paste, but this should really
Devlin
2015/10/28 20:38:17
Done. For my own edification, can you explain why
dcheng
2015/10/30 21:32:59
https://groups.google.com/a/chromium.org/forum/#!t
| |
108 args[0]->ToString(), args[1], v8::ReadOnly); | |
109 if (!result.IsJust() || !result.FromJust()) | |
jochen (gone - plz use gerrit)
2015/10/28 14:22:18
if (!result.FromMaybe(false)) is shorter
Devlin
2015/10/28 20:38:18
Nifty, done.
| |
110 LOG(ERROR) << "Failed to set private property on the export."; | |
111 } | |
112 | |
99 } // namespace | 113 } // namespace |
100 | 114 |
101 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( | 115 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
102 const v8::TryCatch& try_catch) { | 116 const v8::TryCatch& try_catch) { |
103 v8::Local<v8::Message> message(try_catch.Message()); | 117 v8::Local<v8::Message> message(try_catch.Message()); |
104 if (message.IsEmpty()) { | 118 if (message.IsEmpty()) { |
105 return "try_catch has no message"; | 119 return "try_catch has no message"; |
106 } | 120 } |
107 | 121 |
108 std::string resource_name = "<unknown resource>"; | 122 std::string resource_name = "<unknown resource>"; |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 649 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
636 Fatal(context_, "Bad source for require(" + module_name + ")"); | 650 Fatal(context_, "Bad source for require(" + module_name + ")"); |
637 return v8::Undefined(GetIsolate()); | 651 return v8::Undefined(GetIsolate()); |
638 } | 652 } |
639 | 653 |
640 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_as_value); | 654 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_as_value); |
641 | 655 |
642 v8::Local<v8::Object> define_object = v8::Object::New(GetIsolate()); | 656 v8::Local<v8::Object> define_object = v8::Object::New(GetIsolate()); |
643 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); | 657 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); |
644 | 658 |
645 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); | 659 v8::Local<v8::Object> exports = v8::Object::New(GetIsolate()); |
660 | |
661 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( | |
662 GetIsolate(), | |
663 &SetExportsProperty); | |
664 v8::Local<v8::String> v8_key; | |
665 if (!v8_helpers::ToV8String(GetIsolate(), "$set", &v8_key)) { | |
666 NOTREACHED(); | |
667 return v8::Undefined(GetIsolate()); | |
668 } | |
669 | |
670 v8::Local<v8::Function> function; | |
671 if (!tmpl->GetFunction(v8_context).ToLocal(&function)) { | |
672 NOTREACHED(); | |
673 return v8::Undefined(GetIsolate()); | |
674 } | |
675 | |
676 exports->ForceSet(v8_key, function, v8::ReadOnly); | |
677 | |
646 v8::Local<v8::Object> natives(NewInstance()); | 678 v8::Local<v8::Object> natives(NewInstance()); |
647 CHECK(!natives.IsEmpty()); // this can fail if v8 has issues | 679 CHECK(!natives.IsEmpty()); // this can fail if v8 has issues |
648 | 680 |
649 // These must match the argument order in WrapSource. | 681 // These must match the argument order in WrapSource. |
650 v8::Local<v8::Value> args[] = { | 682 v8::Local<v8::Value> args[] = { |
651 // AMD. | 683 // AMD. |
652 GetPropertyUnsafe(v8_context, define_object, "define"), | 684 GetPropertyUnsafe(v8_context, define_object, "define"), |
653 // CommonJS. | 685 // CommonJS. |
654 GetPropertyUnsafe(v8_context, natives, "require", | 686 GetPropertyUnsafe(v8_context, natives, "require", |
655 v8::NewStringType::kInternalized), | 687 v8::NewStringType::kInternalized), |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
717 | 749 |
718 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { | 750 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { |
719 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); | 751 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); |
720 if (existing_handler != native_handler_map_.end()) { | 752 if (existing_handler != native_handler_map_.end()) { |
721 clobbered_native_handlers_.push_back(existing_handler->second); | 753 clobbered_native_handlers_.push_back(existing_handler->second); |
722 native_handler_map_.erase(existing_handler); | 754 native_handler_map_.erase(existing_handler); |
723 } | 755 } |
724 } | 756 } |
725 | 757 |
726 } // namespace extensions | 758 } // namespace extensions |
OLD | NEW |