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

Side by Side Diff: src/api.cc

Issue 1423723002: Map v8::Function to JSReceiver + IsCallable (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates Created 5 years, 2 months 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
« no previous file with comments | « src/api.h ('k') | src/debug/debug.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 1962 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 isolate->ReportPendingMessages(); 1973 isolate->ReportPendingMessages();
1974 } 1974 }
1975 RETURN_ON_FAILED_EXECUTION(Function); 1975 RETURN_ON_FAILED_EXECUTION(Function);
1976 1976
1977 i::Handle<i::Object> result; 1977 i::Handle<i::Object> result;
1978 has_pending_exception = 1978 has_pending_exception =
1979 !i::Execution::Call(isolate, fun, 1979 !i::Execution::Call(isolate, fun,
1980 Utils::OpenHandle(*v8_context->Global()), 0, 1980 Utils::OpenHandle(*v8_context->Global()), 0,
1981 nullptr).ToHandle(&result); 1981 nullptr).ToHandle(&result);
1982 RETURN_ON_FAILED_EXECUTION(Function); 1982 RETURN_ON_FAILED_EXECUTION(Function);
1983 RETURN_ESCAPED(Utils::ToLocal(i::Handle<i::JSFunction>::cast(result))); 1983 RETURN_ESCAPED(
1984 Utils::FunctionToLocal(i::Handle<i::JSFunction>::cast(result)));
1984 } 1985 }
1985 1986
1986 1987
1987 Local<Function> ScriptCompiler::CompileFunctionInContext( 1988 Local<Function> ScriptCompiler::CompileFunctionInContext(
1988 Isolate* v8_isolate, Source* source, Local<Context> v8_context, 1989 Isolate* v8_isolate, Source* source, Local<Context> v8_context,
1989 size_t arguments_count, Local<String> arguments[], 1990 size_t arguments_count, Local<String> arguments[],
1990 size_t context_extension_count, Local<Object> context_extensions[]) { 1991 size_t context_extension_count, Local<Object> context_extensions[]) {
1991 RETURN_TO_LOCAL_UNCHECKED( 1992 RETURN_TO_LOCAL_UNCHECKED(
1992 CompileFunctionInContext(v8_context, source, arguments_count, arguments, 1993 CompileFunctionInContext(v8_context, source, arguments_count, arguments,
1993 context_extension_count, context_extensions), 1994 context_extension_count, context_extensions),
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
2691 bool Value::IsTrue() const { 2692 bool Value::IsTrue() const {
2692 return Utils::OpenHandle(this)->IsTrue(); 2693 return Utils::OpenHandle(this)->IsTrue();
2693 } 2694 }
2694 2695
2695 2696
2696 bool Value::IsFalse() const { 2697 bool Value::IsFalse() const {
2697 return Utils::OpenHandle(this)->IsFalse(); 2698 return Utils::OpenHandle(this)->IsFalse();
2698 } 2699 }
2699 2700
2700 2701
2701 bool Value::IsFunction() const { 2702 bool Value::IsFunction() const { return Utils::OpenHandle(this)->IsCallable(); }
2702 return Utils::OpenHandle(this)->IsJSFunction();
2703 }
2704 2703
2705 2704
2706 bool Value::IsName() const { 2705 bool Value::IsName() const {
2707 return Utils::OpenHandle(this)->IsName(); 2706 return Utils::OpenHandle(this)->IsName();
2708 } 2707 }
2709 2708
2710 2709
2711 bool Value::FullIsString() const { 2710 bool Value::FullIsString() const {
2712 bool result = Utils::OpenHandle(this)->IsString(); 2711 bool result = Utils::OpenHandle(this)->IsString();
2713 DCHECK_EQ(result, QuickIsString()); 2712 DCHECK_EQ(result, QuickIsString());
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
3032 void v8::Object::CheckCast(Value* that) { 3031 void v8::Object::CheckCast(Value* that) {
3033 i::Handle<i::Object> obj = Utils::OpenHandle(that); 3032 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3034 Utils::ApiCheck(obj->IsJSObject(), 3033 Utils::ApiCheck(obj->IsJSObject(),
3035 "v8::Object::Cast()", 3034 "v8::Object::Cast()",
3036 "Could not convert to object"); 3035 "Could not convert to object");
3037 } 3036 }
3038 3037
3039 3038
3040 void v8::Function::CheckCast(Value* that) { 3039 void v8::Function::CheckCast(Value* that) {
3041 i::Handle<i::Object> obj = Utils::OpenHandle(that); 3040 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3042 Utils::ApiCheck(obj->IsJSFunction(), 3041 Utils::ApiCheck(obj->IsCallable(), "v8::Function::Cast()",
3043 "v8::Function::Cast()",
3044 "Could not convert to function"); 3042 "Could not convert to function");
3045 } 3043 }
3046 3044
3047 3045
3048 void v8::Boolean::CheckCast(v8::Value* that) { 3046 void v8::Boolean::CheckCast(v8::Value* that) {
3049 i::Handle<i::Object> obj = Utils::OpenHandle(that); 3047 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3050 Utils::ApiCheck(obj->IsBoolean(), 3048 Utils::ApiCheck(obj->IsBoolean(),
3051 "v8::Boolean::Cast()", 3049 "v8::Boolean::Cast()",
3052 "Could not convert to boolean"); 3050 "Could not convert to boolean");
3053 } 3051 }
(...skipping 1281 matching lines...) Expand 10 before | Expand all | Expand 10 after
4335 4333
4336 MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc, 4334 MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc,
4337 v8::Local<v8::Value> argv[]) const { 4335 v8::Local<v8::Value> argv[]) const {
4338 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Function::NewInstance()", 4336 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Function::NewInstance()",
4339 Object); 4337 Object);
4340 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate); 4338 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4341 auto self = Utils::OpenHandle(this); 4339 auto self = Utils::OpenHandle(this);
4342 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**)); 4340 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
4343 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv); 4341 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4344 Local<Object> result; 4342 Local<Object> result;
4345 has_pending_exception = 4343 has_pending_exception = !ToLocal<Object>(
4346 !ToLocal<Object>(i::Execution::New(self, argc, args), &result); 4344 i::Execution::New(isolate, self, self, argc, args), &result);
4347 RETURN_ON_FAILED_EXECUTION(Object); 4345 RETURN_ON_FAILED_EXECUTION(Object);
4348 RETURN_ESCAPED(result); 4346 RETURN_ESCAPED(result);
4349 } 4347 }
4350 4348
4351 4349
4352 Local<v8::Object> Function::NewInstance(int argc, 4350 Local<v8::Object> Function::NewInstance(int argc,
4353 v8::Local<v8::Value> argv[]) const { 4351 v8::Local<v8::Value> argv[]) const {
4354 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); 4352 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4355 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context, argc, argv), Object); 4353 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context, argc, argv), Object);
4356 } 4354 }
(...skipping 17 matching lines...) Expand all
4374 4372
4375 4373
4376 Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc, 4374 Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc,
4377 v8::Local<v8::Value> argv[]) { 4375 v8::Local<v8::Value> argv[]) {
4378 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); 4376 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4379 RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value); 4377 RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value);
4380 } 4378 }
4381 4379
4382 4380
4383 void Function::SetName(v8::Local<v8::String> name) { 4381 void Function::SetName(v8::Local<v8::String> name) {
4384 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4382 auto self = Utils::OpenHandle(this);
4383 if (!self->IsJSFunction()) return;
4384 auto func = i::Handle<i::JSFunction>::cast(self);
4385 func->shared()->set_name(*Utils::OpenHandle(*name)); 4385 func->shared()->set_name(*Utils::OpenHandle(*name));
4386 } 4386 }
4387 4387
4388 4388
4389 Local<Value> Function::GetName() const { 4389 Local<Value> Function::GetName() const {
4390 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4390 auto self = Utils::OpenHandle(this);
4391 if (!self->IsJSFunction()) {
4392 return ToApiHandle<Primitive>(
4393 self->GetIsolate()->factory()->undefined_value());
4394 }
4395 auto func = i::Handle<i::JSFunction>::cast(self);
4391 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name(), 4396 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name(),
4392 func->GetIsolate())); 4397 func->GetIsolate()));
4393 } 4398 }
4394 4399
4395 4400
4396 Local<Value> Function::GetInferredName() const { 4401 Local<Value> Function::GetInferredName() const {
4397 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4402 auto self = Utils::OpenHandle(this);
4403 if (!self->IsJSFunction()) {
4404 return ToApiHandle<Primitive>(
4405 self->GetIsolate()->factory()->undefined_value());
4406 }
4407 auto func = i::Handle<i::JSFunction>::cast(self);
4398 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(), 4408 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
4399 func->GetIsolate())); 4409 func->GetIsolate()));
4400 } 4410 }
4401 4411
4402 4412
4403 Local<Value> Function::GetDisplayName() const { 4413 Local<Value> Function::GetDisplayName() const {
4404 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); 4414 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4405 ENTER_V8(isolate); 4415 ENTER_V8(isolate);
4406 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4416 auto self = Utils::OpenHandle(this);
4417 if (!self->IsJSFunction()) {
4418 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
4419 }
4420 auto func = i::Handle<i::JSFunction>::cast(self);
4407 i::Handle<i::String> property_name = 4421 i::Handle<i::String> property_name =
4408 isolate->factory()->NewStringFromStaticChars("displayName"); 4422 isolate->factory()->NewStringFromStaticChars("displayName");
4409 i::Handle<i::Object> value = 4423 i::Handle<i::Object> value =
4410 i::JSReceiver::GetDataProperty(func, property_name); 4424 i::JSReceiver::GetDataProperty(func, property_name);
4411 if (value->IsString()) { 4425 if (value->IsString()) {
4412 i::Handle<i::String> name = i::Handle<i::String>::cast(value); 4426 i::Handle<i::String> name = i::Handle<i::String>::cast(value);
4413 if (name->length() > 0) return Utils::ToLocal(name); 4427 if (name->length() > 0) return Utils::ToLocal(name);
4414 } 4428 }
4415 return ToApiHandle<Primitive>(isolate->factory()->undefined_value()); 4429 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
4416 } 4430 }
4417 4431
4418 4432
4419 ScriptOrigin Function::GetScriptOrigin() const { 4433 ScriptOrigin Function::GetScriptOrigin() const {
4420 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4434 auto self = Utils::OpenHandle(this);
4435 if (!self->IsJSFunction()) {
4436 return v8::ScriptOrigin(Local<Value>());
4437 }
4438 auto func = i::Handle<i::JSFunction>::cast(self);
4421 if (func->shared()->script()->IsScript()) { 4439 if (func->shared()->script()->IsScript()) {
4422 i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); 4440 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4423 return GetScriptOriginForScript(func->GetIsolate(), script); 4441 return GetScriptOriginForScript(func->GetIsolate(), script);
4424 } 4442 }
4425 return v8::ScriptOrigin(Local<Value>()); 4443 return v8::ScriptOrigin(Local<Value>());
4426 } 4444 }
4427 4445
4428 4446
4429 const int Function::kLineOffsetNotFound = -1; 4447 const int Function::kLineOffsetNotFound = -1;
4430 4448
4431 4449
4432 int Function::GetScriptLineNumber() const { 4450 int Function::GetScriptLineNumber() const {
4433 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4451 auto self = Utils::OpenHandle(this);
4452 if (!self->IsJSFunction()) {
4453 return kLineOffsetNotFound;
4454 }
4455 auto func = i::Handle<i::JSFunction>::cast(self);
4434 if (func->shared()->script()->IsScript()) { 4456 if (func->shared()->script()->IsScript()) {
4435 i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); 4457 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4436 return i::Script::GetLineNumber(script, func->shared()->start_position()); 4458 return i::Script::GetLineNumber(script, func->shared()->start_position());
4437 } 4459 }
4438 return kLineOffsetNotFound; 4460 return kLineOffsetNotFound;
4439 } 4461 }
4440 4462
4441 4463
4442 int Function::GetScriptColumnNumber() const { 4464 int Function::GetScriptColumnNumber() const {
4443 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4465 auto self = Utils::OpenHandle(this);
4466 if (!self->IsJSFunction()) {
4467 return kLineOffsetNotFound;
4468 }
4469 auto func = i::Handle<i::JSFunction>::cast(self);
4444 if (func->shared()->script()->IsScript()) { 4470 if (func->shared()->script()->IsScript()) {
4445 i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); 4471 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4446 return i::Script::GetColumnNumber(script, func->shared()->start_position()); 4472 return i::Script::GetColumnNumber(script, func->shared()->start_position());
4447 } 4473 }
4448 return kLineOffsetNotFound; 4474 return kLineOffsetNotFound;
4449 } 4475 }
4450 4476
4451 4477
4452 bool Function::IsBuiltin() const { 4478 bool Function::IsBuiltin() const {
4453 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4479 auto self = Utils::OpenHandle(this);
4480 if (!self->IsJSFunction()) {
4481 return false;
4482 }
4483 auto func = i::Handle<i::JSFunction>::cast(self);
4454 return func->IsBuiltin(); 4484 return func->IsBuiltin();
4455 } 4485 }
4456 4486
4457 4487
4458 int Function::ScriptId() const { 4488 int Function::ScriptId() const {
4459 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4489 auto self = Utils::OpenHandle(this);
4490 if (!self->IsJSFunction()) {
4491 return v8::UnboundScript::kNoScriptId;
4492 }
4493 auto func = i::Handle<i::JSFunction>::cast(self);
4460 if (!func->shared()->script()->IsScript()) { 4494 if (!func->shared()->script()->IsScript()) {
4461 return v8::UnboundScript::kNoScriptId; 4495 return v8::UnboundScript::kNoScriptId;
4462 } 4496 }
4463 i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); 4497 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4464 return script->id(); 4498 return script->id();
4465 } 4499 }
4466 4500
4467 4501
4468 Local<v8::Value> Function::GetBoundFunction() const { 4502 Local<v8::Value> Function::GetBoundFunction() const {
4469 i::Handle<i::JSFunction> func = Utils::OpenHandle(this); 4503 auto self = Utils::OpenHandle(this);
4504 if (!self->IsJSFunction()) {
4505 return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
4506 }
4507 auto func = i::Handle<i::JSFunction>::cast(self);
4470 if (!func->shared()->bound()) { 4508 if (!func->shared()->bound()) {
4471 return v8::Undefined(reinterpret_cast<v8::Isolate*>(func->GetIsolate())); 4509 return v8::Undefined(reinterpret_cast<v8::Isolate*>(func->GetIsolate()));
4472 } 4510 }
4473 i::Handle<i::BindingsArray> bound_args = i::Handle<i::BindingsArray>( 4511 i::Handle<i::BindingsArray> bound_args = i::Handle<i::BindingsArray>(
4474 i::BindingsArray::cast(func->function_bindings())); 4512 i::BindingsArray::cast(func->function_bindings()));
4475 i::Handle<i::Object> original(bound_args->bound_function(), 4513 i::Handle<i::Object> original(bound_args->bound_function(),
4476 func->GetIsolate()); 4514 func->GetIsolate());
4477 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(original)); 4515 return Utils::FunctionToLocal(i::Handle<i::JSFunction>::cast(original));
4478 } 4516 }
4479 4517
4480 4518
4481 int Name::GetIdentityHash() { 4519 int Name::GetIdentityHash() {
4482 auto self = Utils::OpenHandle(this); 4520 auto self = Utils::OpenHandle(this);
4483 return static_cast<int>(self->Hash()); 4521 return static_cast<int>(self->Hash());
4484 } 4522 }
4485 4523
4486 4524
4487 int String::Length() const { 4525 int String::Length() const {
(...skipping 3207 matching lines...) Expand 10 before | Expand all | Expand 10 after
7695 7733
7696 MaybeLocal<Value> Debug::GetMirror(Local<Context> context, 7734 MaybeLocal<Value> Debug::GetMirror(Local<Context> context,
7697 v8::Local<v8::Value> obj) { 7735 v8::Local<v8::Value> obj) {
7698 PREPARE_FOR_EXECUTION(context, "v8::Debug::GetMirror()", Value); 7736 PREPARE_FOR_EXECUTION(context, "v8::Debug::GetMirror()", Value);
7699 i::Debug* isolate_debug = isolate->debug(); 7737 i::Debug* isolate_debug = isolate->debug();
7700 has_pending_exception = !isolate_debug->Load(); 7738 has_pending_exception = !isolate_debug->Load();
7701 RETURN_ON_FAILED_EXECUTION(Value); 7739 RETURN_ON_FAILED_EXECUTION(Value);
7702 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global_object()); 7740 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global_object());
7703 auto name = isolate->factory()->NewStringFromStaticChars("MakeMirror"); 7741 auto name = isolate->factory()->NewStringFromStaticChars("MakeMirror");
7704 auto fun_obj = i::Object::GetProperty(debug, name).ToHandleChecked(); 7742 auto fun_obj = i::Object::GetProperty(debug, name).ToHandleChecked();
7705 auto v8_fun = Utils::ToLocal(i::Handle<i::JSFunction>::cast(fun_obj)); 7743 auto v8_fun = Utils::FunctionToLocal(i::Handle<i::JSFunction>::cast(fun_obj));
7706 const int kArgc = 1; 7744 const int kArgc = 1;
7707 v8::Local<v8::Value> argv[kArgc] = {obj}; 7745 v8::Local<v8::Value> argv[kArgc] = {obj};
7708 Local<Value> result; 7746 Local<Value> result;
7709 has_pending_exception = !v8_fun->Call(context, Utils::ToLocal(debug), kArgc, 7747 has_pending_exception = !v8_fun->Call(context, Utils::ToLocal(debug), kArgc,
7710 argv).ToLocal(&result); 7748 argv).ToLocal(&result);
7711 RETURN_ON_FAILED_EXECUTION(Value); 7749 RETURN_ON_FAILED_EXECUTION(Value);
7712 RETURN_ESCAPED(result); 7750 RETURN_ESCAPED(result);
7713 } 7751 }
7714 7752
7715 7753
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
8387 Address callback_address = 8425 Address callback_address =
8388 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8426 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8389 VMState<EXTERNAL> state(isolate); 8427 VMState<EXTERNAL> state(isolate);
8390 ExternalCallbackScope call_scope(isolate, callback_address); 8428 ExternalCallbackScope call_scope(isolate, callback_address);
8391 callback(info); 8429 callback(info);
8392 } 8430 }
8393 8431
8394 8432
8395 } // namespace internal 8433 } // namespace internal
8396 } // namespace v8 8434 } // namespace v8
OLDNEW
« no previous file with comments | « src/api.h ('k') | src/debug/debug.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698