Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: src/api.cc

Issue 1407313004: Adds the possibility of setting a Code object as the callback of a FunctionTemplate. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project 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 "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 988
989 void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) { 989 void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) {
990 auto info = Utils::OpenHandle(this); 990 auto info = Utils::OpenHandle(this);
991 EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit"); 991 EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit");
992 i::Isolate* isolate = info->GetIsolate(); 992 i::Isolate* isolate = info->GetIsolate();
993 ENTER_V8(isolate); 993 ENTER_V8(isolate);
994 info->set_parent_template(*Utils::OpenHandle(*value)); 994 info->set_parent_template(*Utils::OpenHandle(*value));
995 } 995 }
996 996
997 997
998 template<typename CodeOrCallback>
999 static bool IsEmpty(CodeOrCallback callback);
1000
1001
1002 template<>
1003 bool IsEmpty(FunctionCallback callback) {
1004 return callback == 0;
1005 }
1006
1007
1008 #ifdef V8_JS_ACCESSORS
1009 template<>
1010 bool IsEmpty(v8::Local<Value> callback) {
1011 return callback.IsEmpty();
1012 }
1013 #endif
1014
1015
1016 template<typename CodeOrCallback>
998 static Local<FunctionTemplate> FunctionTemplateNew( 1017 static Local<FunctionTemplate> FunctionTemplateNew(
999 i::Isolate* isolate, FunctionCallback callback, v8::Local<Value> data, 1018 i::Isolate* isolate, CodeOrCallback callback, v8::Local<Value> data,
1000 v8::Local<Signature> signature, int length, bool do_not_cache) { 1019 v8::Local<Signature> signature, int length, bool do_not_cache) {
1001 i::Handle<i::Struct> struct_obj = 1020 i::Handle<i::Struct> struct_obj =
1002 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE); 1021 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
1003 i::Handle<i::FunctionTemplateInfo> obj = 1022 i::Handle<i::FunctionTemplateInfo> obj =
1004 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj); 1023 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
1005 InitializeFunctionTemplate(obj); 1024 InitializeFunctionTemplate(obj);
1006 obj->set_do_not_cache(do_not_cache); 1025 obj->set_do_not_cache(do_not_cache);
1007 int next_serial_number = 0; 1026 int next_serial_number = 0;
1008 if (!do_not_cache) { 1027 if (!do_not_cache) {
1009 next_serial_number = isolate->next_serial_number() + 1; 1028 next_serial_number = isolate->next_serial_number() + 1;
1010 isolate->set_next_serial_number(next_serial_number); 1029 isolate->set_next_serial_number(next_serial_number);
1011 } 1030 }
1012 obj->set_serial_number(i::Smi::FromInt(next_serial_number)); 1031 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1013 if (callback != 0) { 1032 if (!IsEmpty(callback)) {
1014 if (data.IsEmpty()) { 1033 if (data.IsEmpty()) {
1015 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); 1034 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1016 } 1035 }
1017 Utils::ToLocal(obj)->SetCallHandler(callback, data); 1036 Utils::ToLocal(obj)->SetCallHandler(callback, data);
1018 } 1037 }
1019 obj->set_length(length); 1038 obj->set_length(length);
1020 obj->set_undetectable(false); 1039 obj->set_undetectable(false);
1021 obj->set_needs_access_check(false); 1040 obj->set_needs_access_check(false);
1022 obj->set_accept_any_receiver(true); 1041 obj->set_accept_any_receiver(true);
1023 if (!signature.IsEmpty()) 1042 if (!signature.IsEmpty())
1024 obj->set_signature(*Utils::OpenHandle(*signature)); 1043 obj->set_signature(*Utils::OpenHandle(*signature));
1025 return Utils::ToLocal(obj); 1044 return Utils::ToLocal(obj);
1026 } 1045 }
1027 1046
1047
1028 Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate, 1048 Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate,
1029 FunctionCallback callback, 1049 FunctionCallback callback,
1030 v8::Local<Value> data, 1050 v8::Local<Value> data,
1031 v8::Local<Signature> signature, 1051 v8::Local<Signature> signature,
1032 int length) { 1052 int length) {
1033 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 1053 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1034 // Changes to the environment cannot be captured in the snapshot. Expect no 1054 // Changes to the environment cannot be captured in the snapshot. Expect no
1035 // function templates when the isolate is created for serialization. 1055 // function templates when the isolate is created for serialization.
1036 DCHECK(!i_isolate->serializer_enabled()); 1056 DCHECK(!i_isolate->serializer_enabled());
1037 LOG_API(i_isolate, "FunctionTemplate::New"); 1057 LOG_API(i_isolate, "FunctionTemplate::New");
1038 ENTER_V8(i_isolate); 1058 ENTER_V8(i_isolate);
1039 return FunctionTemplateNew( 1059 return FunctionTemplateNew(
1040 i_isolate, callback, data, signature, length, false); 1060 i_isolate, callback, data, signature, length, false);
1041 } 1061 }
1042 1062
1043 1063
1064 #ifdef V8_JS_ACCESSORS
1065 Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate,
1066 v8::Local<Value> code,
1067 v8::Local<Value> data,
1068 v8::Local<Signature> signature,
1069 int length) {
1070 // TODO(vogelheim): 'code' should have a more specific type than Local<Value>.
1071 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1072 DCHECK(!i_isolate->serializer_enabled());
1073 LOG_API(i_isolate, "FunctionTemplate::New");
1074 ENTER_V8(i_isolate);
1075 return FunctionTemplateNew(
1076 i_isolate, code, data, signature, length, false);
1077 }
1078 #endif
1079
1080
1044 Local<Signature> Signature::New(Isolate* isolate, 1081 Local<Signature> Signature::New(Isolate* isolate,
1045 Local<FunctionTemplate> receiver) { 1082 Local<FunctionTemplate> receiver) {
1046 return Utils::SignatureToLocal(Utils::OpenHandle(*receiver)); 1083 return Utils::SignatureToLocal(Utils::OpenHandle(*receiver));
1047 } 1084 }
1048 1085
1049 1086
1050 Local<AccessorSignature> AccessorSignature::New( 1087 Local<AccessorSignature> AccessorSignature::New(
1051 Isolate* isolate, Local<FunctionTemplate> receiver) { 1088 Isolate* isolate, Local<FunctionTemplate> receiver) {
1052 return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver)); 1089 return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
1053 } 1090 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 i::Handle<i::CallHandlerInfo>::cast(struct_obj); 1144 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1108 SET_FIELD_WRAPPED(obj, set_callback, callback); 1145 SET_FIELD_WRAPPED(obj, set_callback, callback);
1109 if (data.IsEmpty()) { 1146 if (data.IsEmpty()) {
1110 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); 1147 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1111 } 1148 }
1112 obj->set_data(*Utils::OpenHandle(*data)); 1149 obj->set_data(*Utils::OpenHandle(*data));
1113 info->set_call_code(*obj); 1150 info->set_call_code(*obj);
1114 } 1151 }
1115 1152
1116 1153
1154 #ifdef V8_JS_ACCESSORS
1155 void FunctionTemplate::SetCallHandler(v8::Local<Value> code,
1156 v8::Local<Value> data) {
1157 auto info = Utils::OpenHandle(this);
1158 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetCallHandler");
1159 i::Isolate* isolate = info->GetIsolate();
1160 ENTER_V8(isolate);
1161 i::HandleScope scope(isolate);
1162 i::Handle<i::Struct> struct_obj =
1163 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1164 i::Handle<i::CallHandlerInfo> obj =
1165 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1166 i::Handle<i::Object> callback = Utils::OpenHandle(*code);
1167 CHECK(callback->IsCode());
1168 obj->set_callback(*callback);
1169 if (data.IsEmpty()) {
1170 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1171 }
1172 obj->set_data(*Utils::OpenHandle(*data));
1173 info->set_call_code(*obj);
1174 }
1175 #endif
1176
1177
1117 static i::Handle<i::AccessorInfo> SetAccessorInfoProperties( 1178 static i::Handle<i::AccessorInfo> SetAccessorInfoProperties(
1118 i::Handle<i::AccessorInfo> obj, v8::Local<Name> name, 1179 i::Handle<i::AccessorInfo> obj, v8::Local<Name> name,
1119 v8::AccessControl settings, v8::PropertyAttribute attributes, 1180 v8::AccessControl settings, v8::PropertyAttribute attributes,
1120 v8::Local<AccessorSignature> signature) { 1181 v8::Local<AccessorSignature> signature) {
1121 obj->set_name(*Utils::OpenHandle(*name)); 1182 obj->set_name(*Utils::OpenHandle(*name));
1122 if (settings & ALL_CAN_READ) obj->set_all_can_read(true); 1183 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1123 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true); 1184 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1124 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes)); 1185 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
1125 if (!signature.IsEmpty()) { 1186 if (!signature.IsEmpty()) {
1126 obj->set_expected_receiver_type(*Utils::OpenHandle(*signature)); 1187 obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
(...skipping 7373 matching lines...) Expand 10 before | Expand all | Expand 10 after
8500 Address callback_address = 8561 Address callback_address =
8501 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8562 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8502 VMState<EXTERNAL> state(isolate); 8563 VMState<EXTERNAL> state(isolate);
8503 ExternalCallbackScope call_scope(isolate, callback_address); 8564 ExternalCallbackScope call_scope(isolate, callback_address);
8504 callback(info); 8565 callback(info);
8505 } 8566 }
8506 8567
8507 8568
8508 } // namespace internal 8569 } // namespace internal
8509 } // namespace v8 8570 } // namespace v8
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/api-natives.cc » ('j') | src/execution.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698