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

Side by Side Diff: src/messages.cc

Issue 2557923005: [wasm] Provide correct eval origin for asm.js code (Closed)
Patch Set: Rebase Created 4 years 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/messages.h ('k') | test/mjsunit/wasm/asm-wasm-exception-in-tonumber.js » ('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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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/messages.h" 5 #include "src/messages.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/execution.h" 10 #include "src/execution.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 Handle<Object> arg = Handle<Object>(message->argument(), isolate); 158 Handle<Object> arg = Handle<Object>(message->argument(), isolate);
159 return MessageTemplate::FormatMessage(isolate, message->type(), arg); 159 return MessageTemplate::FormatMessage(isolate, message->type(), arg);
160 } 160 }
161 161
162 std::unique_ptr<char[]> MessageHandler::GetLocalizedMessage( 162 std::unique_ptr<char[]> MessageHandler::GetLocalizedMessage(
163 Isolate* isolate, Handle<Object> data) { 163 Isolate* isolate, Handle<Object> data) {
164 HandleScope scope(isolate); 164 HandleScope scope(isolate);
165 return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS); 165 return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
166 } 166 }
167 167
168 namespace {
169
170 Object* EvalFromFunctionName(Isolate* isolate, Handle<Script> script) {
171 if (script->eval_from_shared()->IsUndefined(isolate))
172 return isolate->heap()->undefined_value();
173
174 Handle<SharedFunctionInfo> shared(
175 SharedFunctionInfo::cast(script->eval_from_shared()));
176 // Find the name of the function calling eval.
177 if (shared->name()->BooleanValue()) {
178 return shared->name();
179 }
180
181 return shared->inferred_name();
182 }
183
184 Object* EvalFromScript(Isolate* isolate, Handle<Script> script) {
185 if (script->eval_from_shared()->IsUndefined(isolate))
186 return isolate->heap()->undefined_value();
187
188 Handle<SharedFunctionInfo> eval_from_shared(
189 SharedFunctionInfo::cast(script->eval_from_shared()));
190 return eval_from_shared->script()->IsScript()
191 ? eval_from_shared->script()
192 : isolate->heap()->undefined_value();
193 }
194
195 MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) {
196 Handle<Object> sourceURL(script->GetNameOrSourceURL(), isolate);
197 if (!sourceURL->IsUndefined(isolate)) {
198 DCHECK(sourceURL->IsString());
199 return Handle<String>::cast(sourceURL);
200 }
201
202 IncrementalStringBuilder builder(isolate);
203 builder.AppendCString("eval at ");
204
205 Handle<Object> eval_from_function_name =
206 handle(EvalFromFunctionName(isolate, script), isolate);
207 if (eval_from_function_name->BooleanValue()) {
208 Handle<String> str;
209 ASSIGN_RETURN_ON_EXCEPTION(
210 isolate, str, Object::ToString(isolate, eval_from_function_name),
211 String);
212 builder.AppendString(str);
213 } else {
214 builder.AppendCString("<anonymous>");
215 }
216
217 Handle<Object> eval_from_script_obj =
218 handle(EvalFromScript(isolate, script), isolate);
219 if (eval_from_script_obj->IsScript()) {
220 Handle<Script> eval_from_script =
221 Handle<Script>::cast(eval_from_script_obj);
222 builder.AppendCString(" (");
223 if (eval_from_script->compilation_type() == Script::COMPILATION_TYPE_EVAL) {
224 // Eval script originated from another eval.
225 Handle<String> str;
226 ASSIGN_RETURN_ON_EXCEPTION(
227 isolate, str, FormatEvalOrigin(isolate, eval_from_script), String);
228 builder.AppendString(str);
229 } else {
230 DCHECK(eval_from_script->compilation_type() !=
231 Script::COMPILATION_TYPE_EVAL);
232 // eval script originated from "real" source.
233 Handle<Object> name_obj = handle(eval_from_script->name(), isolate);
234 if (eval_from_script->name()->IsString()) {
235 builder.AppendString(Handle<String>::cast(name_obj));
236
237 Script::PositionInfo info;
238 if (Script::GetPositionInfo(eval_from_script, script->GetEvalPosition(),
239 &info, Script::NO_OFFSET)) {
240 builder.AppendCString(":");
241
242 Handle<String> str = isolate->factory()->NumberToString(
243 handle(Smi::FromInt(info.line + 1), isolate));
244 builder.AppendString(str);
245
246 builder.AppendCString(":");
247
248 str = isolate->factory()->NumberToString(
249 handle(Smi::FromInt(info.column + 1), isolate));
250 builder.AppendString(str);
251 }
252 } else {
253 DCHECK(!eval_from_script->name()->IsString());
254 builder.AppendCString("unknown source");
255 }
256 }
257 builder.AppendCString(")");
258 }
259
260 Handle<String> result;
261 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
262 return result;
263 }
264
265 } // namespace
266
267 Handle<Object> StackFrameBase::GetEvalOrigin() {
268 if (!HasScript()) return isolate_->factory()->undefined_value();
269 return FormatEvalOrigin(isolate_, GetScript()).ToHandleChecked();
270 }
271
272 bool StackFrameBase::IsEval() {
273 return HasScript() &&
274 GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL;
275 }
276
168 void JSStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array, 277 void JSStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
169 int frame_ix) { 278 int frame_ix) {
170 DCHECK(!array->IsWasmFrame(frame_ix)); 279 DCHECK(!array->IsWasmFrame(frame_ix));
171 isolate_ = isolate; 280 isolate_ = isolate;
172 receiver_ = handle(array->Receiver(frame_ix), isolate); 281 receiver_ = handle(array->Receiver(frame_ix), isolate);
173 function_ = handle(array->Function(frame_ix), isolate); 282 function_ = handle(array->Function(frame_ix), isolate);
174 code_ = handle(array->Code(frame_ix), isolate); 283 code_ = handle(array->Code(frame_ix), isolate);
175 offset_ = array->Offset(frame_ix)->value(); 284 offset_ = array->Offset(frame_ix)->value();
176 285
177 const int flags = array->Flags(frame_ix)->value(); 286 const int flags = array->Flags(frame_ix)->value();
178 force_constructor_ = (flags & FrameArray::kForceConstructor) != 0; 287 force_constructor_ = (flags & FrameArray::kForceConstructor) != 0;
179 is_strict_ = (flags & FrameArray::kIsStrict) != 0; 288 is_strict_ = (flags & FrameArray::kIsStrict) != 0;
180 } 289 }
181 290
291 JSStackFrame::JSStackFrame() {}
292
182 JSStackFrame::JSStackFrame(Isolate* isolate, Handle<Object> receiver, 293 JSStackFrame::JSStackFrame(Isolate* isolate, Handle<Object> receiver,
183 Handle<JSFunction> function, 294 Handle<JSFunction> function,
184 Handle<AbstractCode> code, int offset) 295 Handle<AbstractCode> code, int offset)
185 : isolate_(isolate), 296 : StackFrameBase(isolate),
186 receiver_(receiver), 297 receiver_(receiver),
187 function_(function), 298 function_(function),
188 code_(code), 299 code_(code),
189 offset_(offset), 300 offset_(offset),
190 force_constructor_(false), 301 force_constructor_(false),
191 is_strict_(false) {} 302 is_strict_(false) {}
192 303
193 JSStackFrame::JSStackFrame() {}
194
195 Handle<Object> JSStackFrame::GetFunction() const { 304 Handle<Object> JSStackFrame::GetFunction() const {
196 return Handle<Object>::cast(function_); 305 return Handle<Object>::cast(function_);
197 } 306 }
198 307
199 Handle<Object> JSStackFrame::GetFileName() { 308 Handle<Object> JSStackFrame::GetFileName() {
200 if (!HasScript()) return isolate_->factory()->null_value(); 309 if (!HasScript()) return isolate_->factory()->null_value();
201 return handle(GetScript()->name(), isolate_); 310 return handle(GetScript()->name(), isolate_);
202 } 311 }
203 312
204 Handle<Object> JSStackFrame::GetFunctionName() { 313 Handle<Object> JSStackFrame::GetFunctionName() {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 // Return null in case of duplicates to avoid confusion. 400 // Return null in case of duplicates to avoid confusion.
292 if (!result.is_null()) return isolate_->factory()->null_value(); 401 if (!result.is_null()) return isolate_->factory()->null_value();
293 result = inner_scope.CloseAndEscape(name_key); 402 result = inner_scope.CloseAndEscape(name_key);
294 } 403 }
295 } 404 }
296 405
297 if (!result.is_null()) return outer_scope.CloseAndEscape(result); 406 if (!result.is_null()) return outer_scope.CloseAndEscape(result);
298 return isolate_->factory()->null_value(); 407 return isolate_->factory()->null_value();
299 } 408 }
300 409
301 namespace {
302
303 Object* EvalFromFunctionName(Isolate* isolate, Handle<Script> script) {
304 if (script->eval_from_shared()->IsUndefined(isolate))
305 return isolate->heap()->undefined_value();
306
307 Handle<SharedFunctionInfo> shared(
308 SharedFunctionInfo::cast(script->eval_from_shared()));
309 // Find the name of the function calling eval.
310 if (shared->name()->BooleanValue()) {
311 return shared->name();
312 }
313
314 return shared->inferred_name();
315 }
316
317 Object* EvalFromScript(Isolate* isolate, Handle<Script> script) {
318 if (script->eval_from_shared()->IsUndefined(isolate))
319 return isolate->heap()->undefined_value();
320
321 Handle<SharedFunctionInfo> eval_from_shared(
322 SharedFunctionInfo::cast(script->eval_from_shared()));
323 return eval_from_shared->script()->IsScript()
324 ? eval_from_shared->script()
325 : isolate->heap()->undefined_value();
326 }
327
328 MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) {
329 Handle<Object> sourceURL(script->GetNameOrSourceURL(), isolate);
330 if (!sourceURL->IsUndefined(isolate)) {
331 DCHECK(sourceURL->IsString());
332 return Handle<String>::cast(sourceURL);
333 }
334
335 IncrementalStringBuilder builder(isolate);
336 builder.AppendCString("eval at ");
337
338 Handle<Object> eval_from_function_name =
339 handle(EvalFromFunctionName(isolate, script), isolate);
340 if (eval_from_function_name->BooleanValue()) {
341 Handle<String> str;
342 ASSIGN_RETURN_ON_EXCEPTION(
343 isolate, str, Object::ToString(isolate, eval_from_function_name),
344 String);
345 builder.AppendString(str);
346 } else {
347 builder.AppendCString("<anonymous>");
348 }
349
350 Handle<Object> eval_from_script_obj =
351 handle(EvalFromScript(isolate, script), isolate);
352 if (eval_from_script_obj->IsScript()) {
353 Handle<Script> eval_from_script =
354 Handle<Script>::cast(eval_from_script_obj);
355 builder.AppendCString(" (");
356 if (eval_from_script->compilation_type() == Script::COMPILATION_TYPE_EVAL) {
357 // Eval script originated from another eval.
358 Handle<String> str;
359 ASSIGN_RETURN_ON_EXCEPTION(
360 isolate, str, FormatEvalOrigin(isolate, eval_from_script), String);
361 builder.AppendString(str);
362 } else {
363 DCHECK(eval_from_script->compilation_type() !=
364 Script::COMPILATION_TYPE_EVAL);
365 // eval script originated from "real" source.
366 Handle<Object> name_obj = handle(eval_from_script->name(), isolate);
367 if (eval_from_script->name()->IsString()) {
368 builder.AppendString(Handle<String>::cast(name_obj));
369
370 Script::PositionInfo info;
371 if (Script::GetPositionInfo(eval_from_script, script->GetEvalPosition(),
372 &info, Script::NO_OFFSET)) {
373 builder.AppendCString(":");
374
375 Handle<String> str = isolate->factory()->NumberToString(
376 handle(Smi::FromInt(info.line + 1), isolate));
377 builder.AppendString(str);
378
379 builder.AppendCString(":");
380
381 str = isolate->factory()->NumberToString(
382 handle(Smi::FromInt(info.column + 1), isolate));
383 builder.AppendString(str);
384 }
385 } else {
386 DCHECK(!eval_from_script->name()->IsString());
387 builder.AppendCString("unknown source");
388 }
389 }
390 builder.AppendCString(")");
391 }
392
393 Handle<String> result;
394 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
395 return result;
396 }
397
398 } // namespace
399
400 Handle<Object> JSStackFrame::GetTypeName() { 410 Handle<Object> JSStackFrame::GetTypeName() {
401 // TODO(jgruber): Check for strict/constructor here as in 411 // TODO(jgruber): Check for strict/constructor here as in
402 // CallSitePrototypeGetThis. 412 // CallSitePrototypeGetThis.
403 413
404 if (receiver_->IsNull(isolate_) || receiver_->IsUndefined(isolate_)) 414 if (receiver_->IsNull(isolate_) || receiver_->IsUndefined(isolate_))
405 return isolate_->factory()->null_value(); 415 return isolate_->factory()->null_value();
406 416
407 if (receiver_->IsJSProxy()) return isolate_->factory()->Proxy_string(); 417 if (receiver_->IsJSProxy()) return isolate_->factory()->Proxy_string();
408 418
409 Handle<JSReceiver> receiver_object = 419 Handle<JSReceiver> receiver_object =
410 Object::ToObject(isolate_, receiver_).ToHandleChecked(); 420 Object::ToObject(isolate_, receiver_).ToHandleChecked();
411 return JSReceiver::GetConstructorName(receiver_object); 421 return JSReceiver::GetConstructorName(receiver_object);
412 } 422 }
413 423
414 Handle<Object> JSStackFrame::GetEvalOrigin() {
415 if (!HasScript()) return isolate_->factory()->undefined_value();
416 return FormatEvalOrigin(isolate_, GetScript()).ToHandleChecked();
417 }
418
419 int JSStackFrame::GetLineNumber() { 424 int JSStackFrame::GetLineNumber() {
420 DCHECK_LE(0, GetPosition()); 425 DCHECK_LE(0, GetPosition());
421 if (HasScript()) return Script::GetLineNumber(GetScript(), GetPosition()) + 1; 426 if (HasScript()) return Script::GetLineNumber(GetScript(), GetPosition()) + 1;
422 return -1; 427 return -1;
423 } 428 }
424 429
425 int JSStackFrame::GetColumnNumber() { 430 int JSStackFrame::GetColumnNumber() {
426 DCHECK_LE(0, GetPosition()); 431 DCHECK_LE(0, GetPosition());
427 if (HasScript()) { 432 if (HasScript()) {
428 return Script::GetColumnNumber(GetScript(), GetPosition()) + 1; 433 return Script::GetColumnNumber(GetScript(), GetPosition()) + 1;
429 } 434 }
430 return -1; 435 return -1;
431 } 436 }
432 437
433 bool JSStackFrame::IsNative() { 438 bool JSStackFrame::IsNative() {
434 return HasScript() && GetScript()->type() == Script::TYPE_NATIVE; 439 return HasScript() && GetScript()->type() == Script::TYPE_NATIVE;
435 } 440 }
436 441
437 bool JSStackFrame::IsToplevel() { 442 bool JSStackFrame::IsToplevel() {
438 return receiver_->IsJSGlobalProxy() || receiver_->IsNull(isolate_) || 443 return receiver_->IsJSGlobalProxy() || receiver_->IsNull(isolate_) ||
439 receiver_->IsUndefined(isolate_); 444 receiver_->IsUndefined(isolate_);
440 } 445 }
441 446
442 bool JSStackFrame::IsEval() {
443 return HasScript() &&
444 GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL;
445 }
446
447 bool JSStackFrame::IsConstructor() { 447 bool JSStackFrame::IsConstructor() {
448 if (force_constructor_) return true; 448 if (force_constructor_) return true;
449 if (!receiver_->IsJSObject()) return false; 449 if (!receiver_->IsJSObject()) return false;
450 Handle<Object> constructor = 450 Handle<Object> constructor =
451 JSReceiver::GetDataProperty(Handle<JSObject>::cast(receiver_), 451 JSReceiver::GetDataProperty(Handle<JSObject>::cast(receiver_),
452 isolate_->factory()->constructor_string()); 452 isolate_->factory()->constructor_string());
453 return constructor.is_identical_to(function_); 453 return constructor.is_identical_to(function_);
454 } 454 }
455 455
456 namespace { 456 namespace {
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 int JSStackFrame::GetPosition() const { return code_->SourcePosition(offset_); } 612 int JSStackFrame::GetPosition() const { return code_->SourcePosition(offset_); }
613 613
614 bool JSStackFrame::HasScript() const { 614 bool JSStackFrame::HasScript() const {
615 return function_->shared()->script()->IsScript(); 615 return function_->shared()->script()->IsScript();
616 } 616 }
617 617
618 Handle<Script> JSStackFrame::GetScript() const { 618 Handle<Script> JSStackFrame::GetScript() const {
619 return handle(Script::cast(function_->shared()->script()), isolate_); 619 return handle(Script::cast(function_->shared()->script()), isolate_);
620 } 620 }
621 621
622 WasmStackFrame::WasmStackFrame() {}
623
622 void WasmStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array, 624 void WasmStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
623 int frame_ix) { 625 int frame_ix) {
624 // This function is called for both wasm and asm.js->wasm frames. 626 // This function is called for both wasm and asm.js->wasm frames.
625 DCHECK(array->IsWasmFrame(frame_ix) || array->IsAsmJsWasmFrame(frame_ix)); 627 DCHECK(array->IsWasmFrame(frame_ix) || array->IsAsmJsWasmFrame(frame_ix));
626 isolate_ = isolate; 628 isolate_ = isolate;
627 wasm_instance_ = handle(array->WasmInstance(frame_ix), isolate); 629 wasm_instance_ = handle(array->WasmInstance(frame_ix), isolate);
628 wasm_func_index_ = array->WasmFunctionIndex(frame_ix)->value(); 630 wasm_func_index_ = array->WasmFunctionIndex(frame_ix)->value();
629 code_ = handle(array->Code(frame_ix), isolate); 631 code_ = handle(array->Code(frame_ix), isolate);
630 offset_ = array->Offset(frame_ix)->value(); 632 offset_ = array->Offset(frame_ix)->value();
631 } 633 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 676
675 int WasmStackFrame::GetPosition() const { 677 int WasmStackFrame::GetPosition() const {
676 // TODO(wasm): Clean this up (bug 5007). 678 // TODO(wasm): Clean this up (bug 5007).
677 return (offset_ < 0) ? (-1 - offset_) : code_->SourcePosition(offset_); 679 return (offset_ < 0) ? (-1 - offset_) : code_->SourcePosition(offset_);
678 } 680 }
679 681
680 Handle<Object> WasmStackFrame::Null() const { 682 Handle<Object> WasmStackFrame::Null() const {
681 return isolate_->factory()->null_value(); 683 return isolate_->factory()->null_value();
682 } 684 }
683 685
686 bool WasmStackFrame::HasScript() const { return true; }
687
688 Handle<Script> WasmStackFrame::GetScript() const {
689 return WasmInstanceObject::cast(*wasm_instance_)
690 ->get_compiled_module()
691 ->script();
692 }
693
694 AsmJsWasmStackFrame::AsmJsWasmStackFrame() {}
695
684 void AsmJsWasmStackFrame::FromFrameArray(Isolate* isolate, 696 void AsmJsWasmStackFrame::FromFrameArray(Isolate* isolate,
685 Handle<FrameArray> array, 697 Handle<FrameArray> array,
686 int frame_ix) { 698 int frame_ix) {
687 DCHECK(array->IsAsmJsWasmFrame(frame_ix)); 699 DCHECK(array->IsAsmJsWasmFrame(frame_ix));
688 WasmStackFrame::FromFrameArray(isolate, array, frame_ix); 700 WasmStackFrame::FromFrameArray(isolate, array, frame_ix);
689 is_at_number_conversion_ = 701 is_at_number_conversion_ =
690 array->Flags(frame_ix)->value() & FrameArray::kAsmJsAtNumberConversion; 702 array->Flags(frame_ix)->value() & FrameArray::kAsmJsAtNumberConversion;
691 } 703 }
692 704
693 Handle<Object> AsmJsWasmStackFrame::GetReceiver() const { 705 Handle<Object> AsmJsWasmStackFrame::GetReceiver() const {
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after
1202 DCHECK(mode != SKIP_UNTIL_SEEN); 1214 DCHECK(mode != SKIP_UNTIL_SEEN);
1203 1215
1204 Handle<Object> no_caller; 1216 Handle<Object> no_caller;
1205 Handle<String> msg = FormatMessage(isolate, template_index, arg0, arg1, arg2); 1217 Handle<String> msg = FormatMessage(isolate, template_index, arg0, arg1, arg2);
1206 return ErrorUtils::Construct(isolate, constructor, constructor, msg, mode, 1218 return ErrorUtils::Construct(isolate, constructor, constructor, msg, mode,
1207 no_caller, false); 1219 no_caller, false);
1208 } 1220 }
1209 1221
1210 } // namespace internal 1222 } // namespace internal
1211 } // namespace v8 1223 } // namespace v8
OLDNEW
« no previous file with comments | « src/messages.h ('k') | test/mjsunit/wasm/asm-wasm-exception-in-tonumber.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698