2011-10-06 09:31:38 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-08-20 07:44:00 +00:00
|
|
|
#include "src/messages.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/execution.h"
|
2015-09-01 09:25:19 +00:00
|
|
|
#include "src/isolate-inl.h"
|
2016-05-03 15:29:28 +00:00
|
|
|
#include "src/keys.h"
|
2015-04-16 07:01:20 +00:00
|
|
|
#include "src/string-builder.h"
|
2016-05-06 09:07:30 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2016-04-19 15:13:08 +00:00
|
|
|
MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
|
|
|
|
int end_pos)
|
|
|
|
: script_(script), start_pos_(start_pos), end_pos_(end_pos) {}
|
|
|
|
MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
|
|
|
|
int end_pos, Handle<JSFunction> function)
|
|
|
|
: script_(script),
|
|
|
|
start_pos_(start_pos),
|
|
|
|
end_pos_(end_pos),
|
|
|
|
function_(function) {}
|
|
|
|
MessageLocation::MessageLocation() : start_pos_(-1), end_pos_(-1) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// If no message listeners have been registered this one is called
|
|
|
|
// by default.
|
2013-02-15 09:27:10 +00:00
|
|
|
void MessageHandler::DefaultMessageReport(Isolate* isolate,
|
|
|
|
const MessageLocation* loc,
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> message_obj) {
|
2015-07-13 12:38:06 +00:00
|
|
|
base::SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (loc == NULL) {
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("%s\n", str.get());
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> data(loc->script()->name(), isolate);
|
2015-07-13 12:38:06 +00:00
|
|
|
base::SmartArrayPointer<char> data_str;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (data->IsString())
|
|
|
|
data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
|
|
|
|
loc->start_pos(), str.get());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 13:31:52 +00:00
|
|
|
Handle<JSMessageObject> MessageHandler::MakeMessageObject(
|
2015-08-21 13:18:54 +00:00
|
|
|
Isolate* isolate, MessageTemplate::Template message,
|
|
|
|
MessageLocation* location, Handle<Object> argument,
|
|
|
|
Handle<JSArray> stack_frames) {
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2011-02-02 13:31:52 +00:00
|
|
|
|
2015-08-21 13:18:54 +00:00
|
|
|
int start = -1;
|
|
|
|
int end = -1;
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<Object> script_handle = factory->undefined_value();
|
2015-08-21 13:18:54 +00:00
|
|
|
if (location != NULL) {
|
|
|
|
start = location->start_pos();
|
|
|
|
end = location->end_pos();
|
|
|
|
script_handle = Script::GetWrapper(location->script());
|
|
|
|
} else {
|
|
|
|
script_handle = Script::GetWrapper(isolate->factory()->empty_script());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-02-02 13:31:52 +00:00
|
|
|
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<Object> stack_frames_handle = stack_frames.is_null()
|
2013-06-04 10:30:05 +00:00
|
|
|
? Handle<Object>::cast(factory->undefined_value())
|
2011-02-02 13:31:52 +00:00
|
|
|
: Handle<Object>::cast(stack_frames);
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
Handle<JSMessageObject> message_obj = factory->NewJSMessageObject(
|
|
|
|
message, argument, start, end, script_handle, stack_frames_handle);
|
2011-02-02 13:31:52 +00:00
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
return message_obj;
|
2008-09-09 18:55:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-28 06:30:08 +00:00
|
|
|
void MessageHandler::ReportMessage(Isolate* isolate, MessageLocation* loc,
|
|
|
|
Handle<JSMessageObject> message) {
|
2011-04-07 19:52:24 +00:00
|
|
|
// We are calling into embedder's code which can throw exceptions.
|
|
|
|
// Thus we need to save current exception state, reset it to the clean one
|
|
|
|
// and ignore scheduled exceptions callbacks can throw.
|
2012-10-19 08:45:24 +00:00
|
|
|
|
|
|
|
// We pass the exception object into the message handler callback though.
|
|
|
|
Object* exception_object = isolate->heap()->undefined_value();
|
|
|
|
if (isolate->has_pending_exception()) {
|
2014-04-08 09:44:24 +00:00
|
|
|
exception_object = isolate->pending_exception();
|
2012-10-19 08:45:24 +00:00
|
|
|
}
|
2015-05-28 06:30:08 +00:00
|
|
|
Handle<Object> exception(exception_object, isolate);
|
2012-10-19 08:45:24 +00:00
|
|
|
|
2011-04-07 19:52:24 +00:00
|
|
|
Isolate::ExceptionScope exception_scope(isolate);
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
isolate->set_external_caught_exception(false);
|
|
|
|
|
2015-05-28 06:30:08 +00:00
|
|
|
// Turn the exception on the message into a string if it is an object.
|
|
|
|
if (message->argument()->IsJSObject()) {
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Handle<Object> argument(message->argument(), isolate);
|
2015-12-09 17:00:47 +00:00
|
|
|
|
|
|
|
MaybeHandle<Object> maybe_stringified;
|
2015-05-28 06:30:08 +00:00
|
|
|
Handle<Object> stringified;
|
2015-12-09 17:00:47 +00:00
|
|
|
// Make sure we don't leak uncaught internally generated Error objects.
|
|
|
|
if (Object::IsErrorObject(isolate, argument)) {
|
|
|
|
Handle<Object> args[] = {argument};
|
|
|
|
maybe_stringified = Execution::TryCall(
|
|
|
|
isolate, isolate->no_side_effects_to_string_fun(),
|
|
|
|
isolate->factory()->undefined_value(), arraysize(args), args);
|
|
|
|
} else {
|
|
|
|
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
|
|
|
|
catcher.SetVerbose(false);
|
|
|
|
catcher.SetCaptureMessage(false);
|
|
|
|
|
|
|
|
maybe_stringified = Object::ToString(isolate, argument);
|
|
|
|
}
|
|
|
|
|
2015-05-28 06:30:08 +00:00
|
|
|
if (!maybe_stringified.ToHandle(&stringified)) {
|
|
|
|
stringified = isolate->factory()->NewStringFromAsciiChecked("exception");
|
|
|
|
}
|
|
|
|
message->set_argument(*stringified);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
|
2015-05-28 06:30:08 +00:00
|
|
|
v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-06-04 10:30:05 +00:00
|
|
|
v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
|
2008-07-03 15:10:15 +00:00
|
|
|
int global_length = global_listeners.length();
|
|
|
|
if (global_length == 0) {
|
2013-02-15 09:27:10 +00:00
|
|
|
DefaultMessageReport(isolate, loc, message);
|
2011-04-07 19:52:24 +00:00
|
|
|
if (isolate->has_scheduled_exception()) {
|
|
|
|
isolate->clear_scheduled_exception();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < global_length; i++) {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (global_listeners.get(i)->IsUndefined()) continue;
|
2013-02-27 14:35:39 +00:00
|
|
|
v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
|
|
|
|
Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::MessageCallback callback =
|
2011-10-28 12:37:29 +00:00
|
|
|
FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
|
2013-02-27 14:35:39 +00:00
|
|
|
Handle<Object> callback_data(listener.get(1), isolate);
|
2011-04-11 16:16:52 +00:00
|
|
|
{
|
|
|
|
// Do not allow exceptions to propagate.
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2013-02-27 14:35:39 +00:00
|
|
|
callback(api_message_obj, callback_data->IsUndefined()
|
|
|
|
? api_exception_obj
|
|
|
|
: v8::Utils::ToLocal(callback_data));
|
2011-04-11 16:16:52 +00:00
|
|
|
}
|
2011-04-07 19:52:24 +00:00
|
|
|
if (isolate->has_scheduled_exception()) {
|
|
|
|
isolate->clear_scheduled_exception();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<String> MessageHandler::GetMessage(Isolate* isolate,
|
|
|
|
Handle<Object> data) {
|
2012-11-09 08:22:02 +00:00
|
|
|
Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data);
|
2015-05-18 08:34:05 +00:00
|
|
|
Handle<Object> arg = Handle<Object>(message->argument(), isolate);
|
|
|
|
return MessageTemplate::FormatMessage(isolate, message->type(), arg);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-13 12:38:06 +00:00
|
|
|
base::SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
|
|
|
|
Isolate* isolate, Handle<Object> data) {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2013-02-25 14:46:09 +00:00
|
|
|
return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
CallSite::CallSite(Isolate* isolate, Handle<JSObject> call_site_obj)
|
|
|
|
: isolate_(isolate) {
|
2015-10-13 10:53:06 +00:00
|
|
|
Handle<Object> maybe_function = JSObject::GetDataProperty(
|
|
|
|
call_site_obj, isolate->factory()->call_site_function_symbol());
|
2016-05-06 09:07:30 +00:00
|
|
|
if (maybe_function->IsJSFunction()) {
|
|
|
|
// javascript
|
|
|
|
fun_ = Handle<JSFunction>::cast(maybe_function);
|
|
|
|
receiver_ = JSObject::GetDataProperty(
|
|
|
|
call_site_obj, isolate->factory()->call_site_receiver_symbol());
|
|
|
|
} else {
|
|
|
|
Handle<Object> maybe_wasm_func_index = JSObject::GetDataProperty(
|
|
|
|
call_site_obj, isolate->factory()->call_site_wasm_func_index_symbol());
|
|
|
|
if (!maybe_wasm_func_index->IsSmi()) {
|
|
|
|
// invalid: neither javascript nor wasm
|
|
|
|
return;
|
|
|
|
}
|
2016-05-23 09:00:07 +00:00
|
|
|
Handle<Object> maybe_wasm_obj = JSObject::GetDataProperty(
|
|
|
|
call_site_obj, isolate->factory()->call_site_wasm_obj_symbol());
|
|
|
|
if (!maybe_wasm_obj->IsJSObject()) {
|
|
|
|
// invalid: neither javascript nor wasm
|
|
|
|
return;
|
|
|
|
}
|
2016-05-06 09:07:30 +00:00
|
|
|
// wasm
|
2016-05-23 09:00:07 +00:00
|
|
|
wasm_obj_ = Handle<JSObject>::cast(maybe_wasm_obj);
|
2016-05-06 09:07:30 +00:00
|
|
|
wasm_func_index_ = Smi::cast(*maybe_wasm_func_index)->value();
|
|
|
|
DCHECK(static_cast<int>(wasm_func_index_) >= 0);
|
|
|
|
}
|
2015-10-13 10:53:06 +00:00
|
|
|
|
2015-12-09 17:28:19 +00:00
|
|
|
CHECK(JSObject::GetDataProperty(
|
|
|
|
call_site_obj, isolate->factory()->call_site_position_symbol())
|
|
|
|
->ToInt32(&pos_));
|
2015-08-21 06:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> CallSite::GetFileName() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript()) return isolate_->factory()->null_value();
|
|
|
|
Object* script = fun_->shared()->script();
|
|
|
|
if (!script->IsScript()) return isolate_->factory()->null_value();
|
|
|
|
return Handle<Object>(Script::cast(script)->name(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> CallSite::GetFunctionName() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (IsWasm()) {
|
2016-05-23 09:00:07 +00:00
|
|
|
MaybeHandle<String> name = wasm::GetWasmFunctionName(
|
|
|
|
Handle<JSObject>::cast(wasm_obj_), wasm_func_index_);
|
2016-05-19 07:52:35 +00:00
|
|
|
if (name.is_null()) return isolate_->factory()->null_value();
|
|
|
|
return name.ToHandleChecked();
|
2016-05-06 09:07:30 +00:00
|
|
|
}
|
2015-11-19 19:32:31 +00:00
|
|
|
Handle<String> result = JSFunction::GetName(fun_);
|
2015-04-28 08:53:12 +00:00
|
|
|
if (result->length() != 0) return result;
|
2015-11-19 19:32:31 +00:00
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> script(fun_->shared()->script(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
if (script->IsScript() &&
|
|
|
|
Handle<Script>::cast(script)->compilation_type() ==
|
|
|
|
Script::COMPILATION_TYPE_EVAL) {
|
2015-08-21 06:44:17 +00:00
|
|
|
return isolate_->factory()->eval_string();
|
2015-04-28 08:53:12 +00:00
|
|
|
}
|
2015-08-21 06:44:17 +00:00
|
|
|
return isolate_->factory()->null_value();
|
2015-04-28 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> CallSite::GetScriptNameOrSourceUrl() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript()) return isolate_->factory()->null_value();
|
|
|
|
Object* script_obj = fun_->shared()->script();
|
|
|
|
if (!script_obj->IsScript()) return isolate_->factory()->null_value();
|
|
|
|
Handle<Script> script(Script::cast(script_obj), isolate_);
|
|
|
|
Object* source_url = script->source_url();
|
|
|
|
if (source_url->IsString()) return Handle<Object>(source_url, isolate_);
|
|
|
|
return Handle<Object>(script->name(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 16:27:49 +00:00
|
|
|
bool CheckMethodName(Isolate* isolate, Handle<JSObject> obj, Handle<Name> name,
|
2015-05-05 13:55:28 +00:00
|
|
|
Handle<JSFunction> fun,
|
|
|
|
LookupIterator::Configuration config) {
|
2015-06-30 16:27:49 +00:00
|
|
|
LookupIterator iter =
|
|
|
|
LookupIterator::PropertyOrElement(isolate, obj, name, config);
|
2015-05-05 13:55:28 +00:00
|
|
|
if (iter.state() == LookupIterator::DATA) {
|
|
|
|
return iter.GetDataValue().is_identical_to(fun);
|
|
|
|
} else if (iter.state() == LookupIterator::ACCESSOR) {
|
|
|
|
Handle<Object> accessors = iter.GetAccessors();
|
|
|
|
if (accessors->IsAccessorPair()) {
|
|
|
|
Handle<AccessorPair> pair = Handle<AccessorPair>::cast(accessors);
|
|
|
|
return pair->getter() == *fun || pair->setter() == *fun;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> CallSite::GetMethodName() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript() || receiver_->IsNull() || receiver_->IsUndefined()) {
|
2016-01-15 12:59:40 +00:00
|
|
|
return isolate_->factory()->null_value();
|
|
|
|
}
|
|
|
|
Handle<JSReceiver> receiver =
|
|
|
|
Object::ToObject(isolate_, receiver_).ToHandleChecked();
|
|
|
|
if (!receiver->IsJSObject()) {
|
2015-08-21 06:44:17 +00:00
|
|
|
return isolate_->factory()->null_value();
|
2015-05-05 13:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSObject> obj = Handle<JSObject>::cast(receiver);
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> function_name(fun_->shared()->name(), isolate_);
|
2015-05-05 13:55:28 +00:00
|
|
|
if (function_name->IsName()) {
|
|
|
|
Handle<Name> name = Handle<Name>::cast(function_name);
|
2016-03-03 20:18:30 +00:00
|
|
|
// ES2015 gives getters and setters name prefixes which must
|
|
|
|
// be stripped to find the property name.
|
|
|
|
if (name->IsString() && FLAG_harmony_function_name) {
|
|
|
|
Handle<String> name_string = Handle<String>::cast(name);
|
|
|
|
if (name_string->IsUtf8EqualTo(CStrVector("get "), true) ||
|
|
|
|
name_string->IsUtf8EqualTo(CStrVector("set "), true)) {
|
|
|
|
name = isolate_->factory()->NewProperSubString(name_string, 4,
|
|
|
|
name_string->length());
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 06:44:17 +00:00
|
|
|
if (CheckMethodName(isolate_, obj, name, fun_,
|
2016-03-03 20:18:30 +00:00
|
|
|
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR)) {
|
2015-05-05 13:55:28 +00:00
|
|
|
return name;
|
2016-03-03 20:18:30 +00:00
|
|
|
}
|
2015-05-05 13:55:28 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
HandleScope outer_scope(isolate_);
|
2015-05-05 13:55:28 +00:00
|
|
|
Handle<Object> result;
|
2015-08-21 06:44:17 +00:00
|
|
|
for (PrototypeIterator iter(isolate_, obj,
|
2015-05-05 13:55:28 +00:00
|
|
|
PrototypeIterator::START_AT_RECEIVER);
|
|
|
|
!iter.IsAtEnd(); iter.Advance()) {
|
|
|
|
Handle<Object> current = PrototypeIterator::GetCurrent(iter);
|
|
|
|
if (!current->IsJSObject()) break;
|
|
|
|
Handle<JSObject> current_obj = Handle<JSObject>::cast(current);
|
|
|
|
if (current_obj->IsAccessCheckNeeded()) break;
|
2016-05-03 15:29:28 +00:00
|
|
|
Handle<FixedArray> keys =
|
|
|
|
KeyAccumulator::GetEnumPropertyKeys(isolate_, current_obj);
|
2015-05-05 13:55:28 +00:00
|
|
|
for (int i = 0; i < keys->length(); i++) {
|
2015-08-21 06:44:17 +00:00
|
|
|
HandleScope inner_scope(isolate_);
|
2015-05-05 13:55:28 +00:00
|
|
|
if (!keys->get(i)->IsName()) continue;
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Name> name_key(Name::cast(keys->get(i)), isolate_);
|
|
|
|
if (!CheckMethodName(isolate_, current_obj, name_key, fun_,
|
2015-05-05 13:55:28 +00:00
|
|
|
LookupIterator::OWN_SKIP_INTERCEPTOR))
|
|
|
|
continue;
|
|
|
|
// Return null in case of duplicates to avoid confusion.
|
2015-08-21 06:44:17 +00:00
|
|
|
if (!result.is_null()) return isolate_->factory()->null_value();
|
2015-05-05 13:55:28 +00:00
|
|
|
result = inner_scope.CloseAndEscape(name_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.is_null()) return outer_scope.CloseAndEscape(result);
|
2015-08-21 06:44:17 +00:00
|
|
|
return isolate_->factory()->null_value();
|
2015-05-05 13:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
int CallSite::GetLineNumber() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (pos_ >= 0 && IsJavaScript()) {
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> script_obj(fun_->shared()->script(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
if (script_obj->IsScript()) {
|
|
|
|
Handle<Script> script = Handle<Script>::cast(script_obj);
|
|
|
|
return Script::GetLineNumber(script, pos_) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
int CallSite::GetColumnNumber() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (pos_ >= 0 && IsJavaScript()) {
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> script_obj(fun_->shared()->script(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
if (script_obj->IsScript()) {
|
|
|
|
Handle<Script> script = Handle<Script>::cast(script_obj);
|
|
|
|
return Script::GetColumnNumber(script, pos_) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
bool CallSite::IsNative() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript()) return false;
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> script(fun_->shared()->script(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
return script->IsScript() &&
|
2015-09-28 13:10:13 +00:00
|
|
|
Handle<Script>::cast(script)->type() == Script::TYPE_NATIVE;
|
2015-04-28 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
bool CallSite::IsToplevel() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (IsWasm()) return false;
|
2015-04-28 08:53:12 +00:00
|
|
|
return receiver_->IsJSGlobalProxy() || receiver_->IsNull() ||
|
|
|
|
receiver_->IsUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
bool CallSite::IsEval() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript()) return false;
|
2015-08-21 06:44:17 +00:00
|
|
|
Handle<Object> script(fun_->shared()->script(), isolate_);
|
2015-04-28 08:53:12 +00:00
|
|
|
return script->IsScript() &&
|
|
|
|
Handle<Script>::cast(script)->compilation_type() ==
|
|
|
|
Script::COMPILATION_TYPE_EVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 06:44:17 +00:00
|
|
|
bool CallSite::IsConstructor() {
|
2016-05-06 09:07:30 +00:00
|
|
|
if (!IsJavaScript() || !receiver_->IsJSObject()) return false;
|
2015-05-05 13:55:28 +00:00
|
|
|
Handle<Object> constructor =
|
2015-05-12 13:52:26 +00:00
|
|
|
JSReceiver::GetDataProperty(Handle<JSObject>::cast(receiver_),
|
2015-08-21 06:44:17 +00:00
|
|
|
isolate_->factory()->constructor_string());
|
2015-05-05 13:55:28 +00:00
|
|
|
return constructor.is_identical_to(fun_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
Handle<String> MessageTemplate::FormatMessage(Isolate* isolate,
|
|
|
|
int template_index,
|
|
|
|
Handle<Object> arg) {
|
|
|
|
Factory* factory = isolate->factory();
|
2015-05-28 06:30:08 +00:00
|
|
|
Handle<String> result_string;
|
|
|
|
if (arg->IsString()) {
|
|
|
|
result_string = Handle<String>::cast(arg);
|
|
|
|
} else {
|
2015-12-09 17:00:47 +00:00
|
|
|
Handle<JSFunction> fun = isolate->no_side_effects_to_string_fun();
|
2015-05-28 06:30:08 +00:00
|
|
|
|
|
|
|
MaybeHandle<Object> maybe_result =
|
2015-10-23 12:26:49 +00:00
|
|
|
Execution::TryCall(isolate, fun, factory->undefined_value(), 1, &arg);
|
2015-05-28 06:30:08 +00:00
|
|
|
Handle<Object> result;
|
|
|
|
if (!maybe_result.ToHandle(&result) || !result->IsString()) {
|
|
|
|
return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>"));
|
|
|
|
}
|
|
|
|
result_string = Handle<String>::cast(result);
|
2015-05-18 08:34:05 +00:00
|
|
|
}
|
|
|
|
MaybeHandle<String> maybe_result_string = MessageTemplate::FormatMessage(
|
2015-05-28 06:30:08 +00:00
|
|
|
template_index, result_string, factory->empty_string(),
|
2015-05-18 08:34:05 +00:00
|
|
|
factory->empty_string());
|
|
|
|
if (!maybe_result_string.ToHandle(&result_string)) {
|
|
|
|
return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>"));
|
|
|
|
}
|
|
|
|
// A string that has been obtained from JS code in this way is
|
|
|
|
// likely to be a complicated ConsString of some sort. We flatten it
|
|
|
|
// here to improve the efficiency of converting it to a C string and
|
|
|
|
// other operations that are likely to take place (see GetLocalizedMessage
|
|
|
|
// for example).
|
|
|
|
return String::Flatten(result_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-17 11:02:31 +00:00
|
|
|
const char* MessageTemplate::TemplateString(int template_index) {
|
2015-04-16 07:01:20 +00:00
|
|
|
switch (template_index) {
|
2015-08-17 11:02:31 +00:00
|
|
|
#define CASE(NAME, STRING) \
|
|
|
|
case k##NAME: \
|
|
|
|
return STRING;
|
2015-04-16 07:01:20 +00:00
|
|
|
MESSAGE_TEMPLATES(CASE)
|
|
|
|
#undef CASE
|
|
|
|
case kLastMessage:
|
|
|
|
default:
|
2015-08-17 11:02:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaybeHandle<String> MessageTemplate::FormatMessage(int template_index,
|
|
|
|
Handle<String> arg0,
|
|
|
|
Handle<String> arg1,
|
|
|
|
Handle<String> arg2) {
|
|
|
|
Isolate* isolate = arg0->GetIsolate();
|
|
|
|
const char* template_string = TemplateString(template_index);
|
|
|
|
if (template_string == NULL) {
|
|
|
|
isolate->ThrowIllegalOperation();
|
|
|
|
return MaybeHandle<String>();
|
2015-04-16 07:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalStringBuilder builder(isolate);
|
|
|
|
|
2015-04-16 07:59:39 +00:00
|
|
|
unsigned int i = 0;
|
2015-04-16 07:01:20 +00:00
|
|
|
Handle<String> args[] = {arg0, arg1, arg2};
|
|
|
|
for (const char* c = template_string; *c != '\0'; c++) {
|
|
|
|
if (*c == '%') {
|
2015-05-18 08:34:05 +00:00
|
|
|
// %% results in verbatim %.
|
|
|
|
if (*(c + 1) == '%') {
|
|
|
|
c++;
|
|
|
|
builder.AppendCharacter('%');
|
|
|
|
} else {
|
|
|
|
DCHECK(i < arraysize(args));
|
2015-06-19 08:31:22 +00:00
|
|
|
Handle<String> arg = args[i++];
|
2015-07-06 10:25:50 +00:00
|
|
|
builder.AppendString(arg);
|
2015-05-18 08:34:05 +00:00
|
|
|
}
|
2015-04-16 07:01:20 +00:00
|
|
|
} else {
|
|
|
|
builder.AppendCharacter(*c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.Finish();
|
|
|
|
}
|
2015-08-11 09:15:27 +00:00
|
|
|
|
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|