OLD | NEW |
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 // ------------------------------------------------------------------- | 5 // ------------------------------------------------------------------- |
6 | 6 |
7 (function(global, utils) { | 7 (function(global, utils) { |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var ArrayJoin; | 14 var MakeGenericError = utils.ImportNow("make_generic_error"); |
15 var Bool16x8ToString; | |
16 var Bool32x4ToString; | |
17 var Bool8x16ToString; | |
18 var CallSite = utils.ImportNow("CallSite"); | |
19 var Float32x4ToString; | |
20 var GlobalObject = global.Object; | |
21 var GlobalError = global.Error; | 15 var GlobalError = global.Error; |
22 var GlobalEvalError = global.EvalError; | |
23 var GlobalRangeError = global.RangeError; | 16 var GlobalRangeError = global.RangeError; |
24 var GlobalReferenceError = global.ReferenceError; | |
25 var GlobalSyntaxError = global.SyntaxError; | 17 var GlobalSyntaxError = global.SyntaxError; |
26 var GlobalTypeError = global.TypeError; | 18 var GlobalTypeError = global.TypeError; |
27 var GlobalURIError = global.URIError; | 19 var GlobalURIError = global.URIError; |
28 var Int16x8ToString; | |
29 var Int32x4ToString; | |
30 var Int8x16ToString; | |
31 var InternalArray = utils.InternalArray; | |
32 var internalErrorSymbol = utils.ImportNow("internal_error_symbol"); | |
33 var ObjectHasOwnProperty; | |
34 var ObjectToString = utils.ImportNow("object_to_string"); | |
35 var Script = utils.ImportNow("Script"); | 20 var Script = utils.ImportNow("Script"); |
36 var stackTraceSymbol = utils.ImportNow("stack_trace_symbol"); | |
37 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
38 var Uint16x8ToString; | |
39 var Uint32x4ToString; | |
40 var Uint8x16ToString; | |
41 | |
42 utils.Import(function(from) { | |
43 ArrayJoin = from.ArrayJoin; | |
44 Bool16x8ToString = from.Bool16x8ToString; | |
45 Bool32x4ToString = from.Bool32x4ToString; | |
46 Bool8x16ToString = from.Bool8x16ToString; | |
47 Float32x4ToString = from.Float32x4ToString; | |
48 Int16x8ToString = from.Int16x8ToString; | |
49 Int32x4ToString = from.Int32x4ToString; | |
50 Int8x16ToString = from.Int8x16ToString; | |
51 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | |
52 Uint16x8ToString = from.Uint16x8ToString; | |
53 Uint32x4ToString = from.Uint32x4ToString; | |
54 Uint8x16ToString = from.Uint8x16ToString; | |
55 }); | |
56 | 21 |
57 // ------------------------------------------------------------------- | 22 // ------------------------------------------------------------------- |
58 | 23 |
59 | |
60 | |
61 function NoSideEffectsObjectToString() { | |
62 if (IS_UNDEFINED(this)) return "[object Undefined]"; | |
63 if (IS_NULL(this)) return "[object Null]"; | |
64 var O = TO_OBJECT(this); | |
65 var builtinTag = %_ClassOf(O); | |
66 var tag = %GetDataProperty(O, toStringTagSymbol); | |
67 if (!IS_STRING(tag)) { | |
68 tag = builtinTag; | |
69 } | |
70 return `[object ${tag}]`; | |
71 } | |
72 | |
73 function IsErrorObject(obj) { | |
74 return HAS_PRIVATE(obj, stackTraceSymbol); | |
75 } | |
76 | |
77 function NoSideEffectsErrorToString() { | |
78 var name = %GetDataProperty(this, "name"); | |
79 var message = %GetDataProperty(this, "message"); | |
80 name = IS_UNDEFINED(name) ? "Error" : NoSideEffectsToString(name); | |
81 message = IS_UNDEFINED(message) ? "" : NoSideEffectsToString(message); | |
82 if (name == "") return message; | |
83 if (message == "") return name; | |
84 return `${name}: ${message}`; | |
85 } | |
86 | |
87 function NoSideEffectsToString(obj) { | |
88 if (IS_STRING(obj)) return obj; | |
89 if (IS_NUMBER(obj)) return %_NumberToString(obj); | |
90 if (IS_BOOLEAN(obj)) return obj ? 'true' : 'false'; | |
91 if (IS_UNDEFINED(obj)) return 'undefined'; | |
92 if (IS_NULL(obj)) return 'null'; | |
93 if (IS_FUNCTION(obj)) { | |
94 var str = %FunctionToString(obj); | |
95 if (str.length > 128) { | |
96 str = %_SubString(str, 0, 111) + "...<omitted>..." + | |
97 %_SubString(str, str.length - 2, str.length); | |
98 } | |
99 return str; | |
100 } | |
101 if (IS_SYMBOL(obj)) return %SymbolDescriptiveString(obj); | |
102 if (IS_SIMD_VALUE(obj)) { | |
103 switch (typeof(obj)) { | |
104 case 'float32x4': return %_Call(Float32x4ToString, obj); | |
105 case 'int32x4': return %_Call(Int32x4ToString, obj); | |
106 case 'int16x8': return %_Call(Int16x8ToString, obj); | |
107 case 'int8x16': return %_Call(Int8x16ToString, obj); | |
108 case 'uint32x4': return %_Call(Uint32x4ToString, obj); | |
109 case 'uint16x8': return %_Call(Uint16x8ToString, obj); | |
110 case 'uint8x16': return %_Call(Uint8x16ToString, obj); | |
111 case 'bool32x4': return %_Call(Bool32x4ToString, obj); | |
112 case 'bool16x8': return %_Call(Bool16x8ToString, obj); | |
113 case 'bool8x16': return %_Call(Bool8x16ToString, obj); | |
114 } | |
115 } | |
116 | |
117 if (IS_RECEIVER(obj)) { | |
118 // When internally formatting error objects, use a side-effects-free version | |
119 // of Error.prototype.toString independent of the actually installed | |
120 // toString method. | |
121 if (IsErrorObject(obj) || | |
122 %GetDataProperty(obj, "toString") === ErrorToString) { | |
123 return %_Call(NoSideEffectsErrorToString, obj); | |
124 } | |
125 | |
126 if (%GetDataProperty(obj, "toString") === ObjectToString) { | |
127 var constructor = %GetDataProperty(obj, "constructor"); | |
128 if (IS_FUNCTION(constructor)) { | |
129 var constructor_name = %FunctionGetName(constructor); | |
130 if (constructor_name != "") return `#<${constructor_name}>`; | |
131 } | |
132 } | |
133 } | |
134 | |
135 return %_Call(NoSideEffectsObjectToString, obj); | |
136 } | |
137 | |
138 | |
139 function MakeGenericError(constructor, type, arg0, arg1, arg2) { | |
140 var error = new constructor(FormatMessage(type, arg0, arg1, arg2)); | |
141 error[internalErrorSymbol] = true; | |
142 return error; | |
143 } | |
144 | |
145 | |
146 /** | 24 /** |
147 * Set up the Script function and constructor. | 25 * Set up the Script function and constructor. |
148 */ | 26 */ |
149 %FunctionSetInstanceClassName(Script, 'Script'); | 27 %FunctionSetInstanceClassName(Script, 'Script'); |
150 %AddNamedProperty(Script.prototype, 'constructor', Script, | 28 %AddNamedProperty(Script.prototype, 'constructor', Script, |
151 DONT_ENUM | DONT_DELETE | READ_ONLY); | 29 DONT_ENUM | DONT_DELETE | READ_ONLY); |
152 %SetCode(Script, function(x) { | 30 %SetCode(Script, function(x) { |
153 // Script objects can only be created by the VM. | 31 // Script objects can only be created by the VM. |
154 throw MakeError(kUnsupported); | 32 throw MakeError(kUnsupported); |
155 }); | 33 }); |
156 | 34 |
157 | |
158 // Helper functions; called from the runtime system. | |
159 function FormatMessage(type, arg0, arg1, arg2) { | |
160 var arg0 = NoSideEffectsToString(arg0); | |
161 var arg1 = NoSideEffectsToString(arg1); | |
162 var arg2 = NoSideEffectsToString(arg2); | |
163 try { | |
164 return %FormatMessageString(type, arg0, arg1, arg2); | |
165 } catch (e) { | |
166 return "<error>"; | |
167 } | |
168 } | |
169 | |
170 | |
171 function GetLineNumber(message) { | 35 function GetLineNumber(message) { |
172 var start_position = %MessageGetStartPosition(message); | 36 var start_position = %MessageGetStartPosition(message); |
173 if (start_position == -1) return kNoLineNumberInfo; | 37 if (start_position == -1) return kNoLineNumberInfo; |
174 var script = %MessageGetScript(message); | 38 var script = %MessageGetScript(message); |
175 var location = script.locationFromPosition(start_position, true); | 39 var location = script.locationFromPosition(start_position, true); |
176 if (location == null) return kNoLineNumberInfo; | 40 if (location == null) return kNoLineNumberInfo; |
177 return location.line + 1; | 41 return location.line + 1; |
178 } | 42 } |
179 | 43 |
180 | 44 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 145 |
282 function MakeTypeError(type, arg0, arg1, arg2) { | 146 function MakeTypeError(type, arg0, arg1, arg2) { |
283 return MakeGenericError(GlobalTypeError, type, arg0, arg1, arg2); | 147 return MakeGenericError(GlobalTypeError, type, arg0, arg1, arg2); |
284 } | 148 } |
285 | 149 |
286 function MakeURIError() { | 150 function MakeURIError() { |
287 return MakeGenericError(GlobalURIError, kURIMalformed); | 151 return MakeGenericError(GlobalURIError, kURIMalformed); |
288 } | 152 } |
289 | 153 |
290 %InstallToContext([ | 154 %InstallToContext([ |
291 "make_error_function", MakeGenericError, | |
292 "make_range_error", MakeRangeError, | 155 "make_range_error", MakeRangeError, |
293 "make_type_error", MakeTypeError, | 156 "make_type_error", MakeTypeError, |
294 "message_get_column_number", GetColumnNumber, | 157 "message_get_column_number", GetColumnNumber, |
295 "message_get_line_number", GetLineNumber, | 158 "message_get_line_number", GetLineNumber, |
296 "message_get_source_line", GetSourceLine, | 159 "message_get_source_line", GetSourceLine, |
297 "no_side_effects_to_string_fun", NoSideEffectsToString, | |
298 ]); | 160 ]); |
299 | 161 |
300 utils.Export(function(to) { | 162 utils.Export(function(to) { |
301 to.ErrorToString = ErrorToString; | 163 to.ErrorToString = ErrorToString; |
302 to.MakeError = MakeError; | 164 to.MakeError = MakeError; |
303 to.MakeRangeError = MakeRangeError; | 165 to.MakeRangeError = MakeRangeError; |
304 to.MakeSyntaxError = MakeSyntaxError; | 166 to.MakeSyntaxError = MakeSyntaxError; |
305 to.MakeTypeError = MakeTypeError; | 167 to.MakeTypeError = MakeTypeError; |
306 to.MakeURIError = MakeURIError; | 168 to.MakeURIError = MakeURIError; |
307 }); | 169 }); |
308 | 170 |
309 }); | 171 }); |
OLD | NEW |