OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/factory.h" | 5 #include "src/factory.h" |
6 | 6 |
7 #include "src/allocation-site-scopes.h" | 7 #include "src/allocation-site-scopes.h" |
8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
9 #include "src/conversions.h" | 9 #include "src/conversions.h" |
10 #include "src/isolate-inl.h" | 10 #include "src/isolate-inl.h" |
(...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1051 Handle<HeapNumber> Factory::NewHeapNumber(double value, | 1051 Handle<HeapNumber> Factory::NewHeapNumber(double value, |
1052 MutableMode mode, | 1052 MutableMode mode, |
1053 PretenureFlag pretenure) { | 1053 PretenureFlag pretenure) { |
1054 CALL_HEAP_FUNCTION( | 1054 CALL_HEAP_FUNCTION( |
1055 isolate(), | 1055 isolate(), |
1056 isolate()->heap()->AllocateHeapNumber(value, mode, pretenure), | 1056 isolate()->heap()->AllocateHeapNumber(value, mode, pretenure), |
1057 HeapNumber); | 1057 HeapNumber); |
1058 } | 1058 } |
1059 | 1059 |
1060 | 1060 |
1061 MaybeHandle<Object> Factory::NewTypeError(const char* message, | 1061 Handle<Object> Factory::NewTypeError(const char* message, |
1062 Vector<Handle<Object> > args) { | 1062 Vector<Handle<Object> > args) { |
1063 return NewError("MakeTypeError", message, args); | 1063 return NewError("MakeTypeError", message, args); |
1064 } | 1064 } |
1065 | 1065 |
1066 | 1066 |
1067 MaybeHandle<Object> Factory::NewTypeError(Handle<String> message) { | 1067 Handle<Object> Factory::NewTypeError(Handle<String> message) { |
1068 return NewError("$TypeError", message); | 1068 return NewError("$TypeError", message); |
1069 } | 1069 } |
1070 | 1070 |
1071 | 1071 |
1072 MaybeHandle<Object> Factory::NewRangeError(const char* message, | 1072 Handle<Object> Factory::NewRangeError(const char* message, |
1073 Vector<Handle<Object> > args) { | 1073 Vector<Handle<Object> > args) { |
1074 return NewError("MakeRangeError", message, args); | 1074 return NewError("MakeRangeError", message, args); |
1075 } | 1075 } |
1076 | 1076 |
1077 | 1077 |
1078 MaybeHandle<Object> Factory::NewRangeError(Handle<String> message) { | 1078 Handle<Object> Factory::NewRangeError(Handle<String> message) { |
1079 return NewError("$RangeError", message); | 1079 return NewError("$RangeError", message); |
1080 } | 1080 } |
1081 | 1081 |
1082 | 1082 |
1083 MaybeHandle<Object> Factory::NewSyntaxError(const char* message, | 1083 Handle<Object> Factory::NewSyntaxError(const char* message, |
1084 Handle<JSArray> args) { | 1084 Handle<JSArray> args) { |
1085 return NewError("MakeSyntaxError", message, args); | 1085 return NewError("MakeSyntaxError", message, args); |
1086 } | 1086 } |
1087 | 1087 |
1088 | 1088 |
1089 MaybeHandle<Object> Factory::NewSyntaxError(Handle<String> message) { | 1089 Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
1090 return NewError("$SyntaxError", message); | 1090 return NewError("$SyntaxError", message); |
1091 } | 1091 } |
1092 | 1092 |
1093 | 1093 |
1094 MaybeHandle<Object> Factory::NewReferenceError(const char* message, | 1094 Handle<Object> Factory::NewReferenceError(const char* message, |
1095 Vector<Handle<Object> > args) { | 1095 Vector<Handle<Object> > args) { |
1096 return NewError("MakeReferenceError", message, args); | 1096 return NewError("MakeReferenceError", message, args); |
1097 } | 1097 } |
1098 | 1098 |
1099 | 1099 |
1100 MaybeHandle<Object> Factory::NewReferenceError(const char* message, | 1100 Handle<Object> Factory::NewReferenceError(const char* message, |
1101 Handle<JSArray> args) { | 1101 Handle<JSArray> args) { |
1102 return NewError("MakeReferenceError", message, args); | 1102 return NewError("MakeReferenceError", message, args); |
1103 } | 1103 } |
1104 | 1104 |
1105 | 1105 |
1106 MaybeHandle<Object> Factory::NewReferenceError(Handle<String> message) { | 1106 Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
1107 return NewError("$ReferenceError", message); | 1107 return NewError("$ReferenceError", message); |
1108 } | 1108 } |
1109 | 1109 |
1110 | 1110 |
1111 MaybeHandle<Object> Factory::NewError(const char* maker, const char* message, | 1111 Handle<Object> Factory::NewError(const char* maker, const char* message, |
1112 Vector<Handle<Object> > args) { | 1112 Vector<Handle<Object> > args) { |
1113 // Instantiate a closeable HandleScope for EscapeFrom. | 1113 // Instantiate a closeable HandleScope for EscapeFrom. |
1114 v8::EscapableHandleScope scope(reinterpret_cast<v8::Isolate*>(isolate())); | 1114 v8::EscapableHandleScope scope(reinterpret_cast<v8::Isolate*>(isolate())); |
1115 Handle<FixedArray> array = NewFixedArray(args.length()); | 1115 Handle<FixedArray> array = NewFixedArray(args.length()); |
1116 for (int i = 0; i < args.length(); i++) { | 1116 for (int i = 0; i < args.length(); i++) { |
1117 array->set(i, *args[i]); | 1117 array->set(i, *args[i]); |
1118 } | 1118 } |
1119 Handle<JSArray> object = NewJSArrayWithElements(array); | 1119 Handle<JSArray> object = NewJSArrayWithElements(array); |
1120 Handle<Object> result; | 1120 Handle<Object> result = NewError(maker, message, object); |
1121 ASSIGN_RETURN_ON_EXCEPTION(isolate(), result, | |
1122 NewError(maker, message, object), Object); | |
1123 return result.EscapeFrom(&scope); | 1121 return result.EscapeFrom(&scope); |
1124 } | 1122 } |
1125 | 1123 |
1126 | 1124 |
1127 MaybeHandle<Object> Factory::NewEvalError(const char* message, | 1125 Handle<Object> Factory::NewEvalError(const char* message, |
1128 Vector<Handle<Object> > args) { | 1126 Vector<Handle<Object> > args) { |
1129 return NewError("MakeEvalError", message, args); | 1127 return NewError("MakeEvalError", message, args); |
1130 } | 1128 } |
1131 | 1129 |
1132 | 1130 |
1133 MaybeHandle<Object> Factory::NewError(const char* message, | 1131 Handle<Object> Factory::NewError(const char* message, |
1134 Vector<Handle<Object> > args) { | 1132 Vector<Handle<Object> > args) { |
1135 return NewError("MakeError", message, args); | 1133 return NewError("MakeError", message, args); |
1136 } | 1134 } |
1137 | 1135 |
1138 | 1136 |
1139 Handle<String> Factory::EmergencyNewError(const char* message, | 1137 Handle<String> Factory::EmergencyNewError(const char* message, |
1140 Handle<JSArray> args) { | 1138 Handle<JSArray> args) { |
1141 const int kBufferSize = 1000; | 1139 const int kBufferSize = 1000; |
1142 char buffer[kBufferSize]; | 1140 char buffer[kBufferSize]; |
1143 size_t space = kBufferSize; | 1141 size_t space = kBufferSize; |
1144 char* p = &buffer[0]; | 1142 char* p = &buffer[0]; |
(...skipping 20 matching lines...) Expand all Loading... |
1165 } | 1163 } |
1166 if (space > 0) { | 1164 if (space > 0) { |
1167 *p = '\0'; | 1165 *p = '\0'; |
1168 } else { | 1166 } else { |
1169 buffer[kBufferSize - 1] = '\0'; | 1167 buffer[kBufferSize - 1] = '\0'; |
1170 } | 1168 } |
1171 return NewStringFromUtf8(CStrVector(buffer), TENURED).ToHandleChecked(); | 1169 return NewStringFromUtf8(CStrVector(buffer), TENURED).ToHandleChecked(); |
1172 } | 1170 } |
1173 | 1171 |
1174 | 1172 |
1175 MaybeHandle<Object> Factory::NewError(const char* maker, const char* message, | 1173 Handle<Object> Factory::NewError(const char* maker, const char* message, |
1176 Handle<JSArray> args) { | 1174 Handle<JSArray> args) { |
1177 Handle<String> make_str = InternalizeUtf8String(maker); | 1175 Handle<String> make_str = InternalizeUtf8String(maker); |
1178 Handle<Object> fun_obj = Object::GetProperty( | 1176 Handle<Object> fun_obj = Object::GetProperty( |
1179 isolate()->js_builtins_object(), make_str).ToHandleChecked(); | 1177 isolate()->js_builtins_object(), make_str).ToHandleChecked(); |
1180 // If the builtins haven't been properly configured yet this error | 1178 // If the builtins haven't been properly configured yet this error |
1181 // constructor may not have been defined. Bail out. | 1179 // constructor may not have been defined. Bail out. |
1182 if (!fun_obj->IsJSFunction()) { | 1180 if (!fun_obj->IsJSFunction()) { |
1183 return EmergencyNewError(message, args); | 1181 return EmergencyNewError(message, args); |
1184 } | 1182 } |
1185 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); | 1183 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
1186 Handle<Object> message_obj = InternalizeUtf8String(message); | 1184 Handle<Object> message_obj = InternalizeUtf8String(message); |
1187 Handle<Object> argv[] = { message_obj, args }; | 1185 Handle<Object> argv[] = { message_obj, args }; |
1188 | 1186 |
1189 // Invoke the JavaScript factory method. If an exception is thrown while | 1187 // Invoke the JavaScript factory method. If an exception is thrown while |
1190 // running the factory method, use the exception as the result. | 1188 // running the factory method, use the exception as the result. |
1191 Handle<Object> result; | 1189 Handle<Object> result; |
1192 MaybeHandle<Object> exception; | 1190 MaybeHandle<Object> exception; |
1193 if (!Execution::TryCall(fun, | 1191 if (!Execution::TryCall(fun, |
1194 isolate()->js_builtins_object(), | 1192 isolate()->js_builtins_object(), |
1195 arraysize(argv), | 1193 arraysize(argv), |
1196 argv, | 1194 argv, |
1197 &exception).ToHandle(&result)) { | 1195 &exception).ToHandle(&result)) { |
1198 Handle<Object> exception_obj; | 1196 Handle<Object> exception_obj; |
1199 if (exception.ToHandle(&exception_obj)) return exception_obj; | 1197 if (exception.ToHandle(&exception_obj)) return exception_obj; |
1200 return undefined_value(); | 1198 return undefined_value(); |
1201 } | 1199 } |
1202 return result; | 1200 return result; |
1203 } | 1201 } |
1204 | 1202 |
1205 | 1203 |
1206 MaybeHandle<Object> Factory::NewError(Handle<String> message) { | 1204 Handle<Object> Factory::NewError(Handle<String> message) { |
1207 return NewError("$Error", message); | 1205 return NewError("$Error", message); |
1208 } | 1206 } |
1209 | 1207 |
1210 | 1208 |
1211 MaybeHandle<Object> Factory::NewError(const char* constructor, | 1209 Handle<Object> Factory::NewError(const char* constructor, |
1212 Handle<String> message) { | 1210 Handle<String> message) { |
1213 Handle<String> constr = InternalizeUtf8String(constructor); | 1211 Handle<String> constr = InternalizeUtf8String(constructor); |
1214 Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty( | 1212 Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty( |
1215 isolate()->js_builtins_object(), constr).ToHandleChecked()); | 1213 isolate()->js_builtins_object(), constr).ToHandleChecked()); |
1216 Handle<Object> argv[] = { message }; | 1214 Handle<Object> argv[] = { message }; |
1217 | 1215 |
1218 // Invoke the JavaScript factory method. If an exception is thrown while | 1216 // Invoke the JavaScript factory method. If an exception is thrown while |
1219 // running the factory method, use the exception as the result. | 1217 // running the factory method, use the exception as the result. |
1220 Handle<Object> result; | 1218 Handle<Object> result; |
1221 MaybeHandle<Object> exception; | 1219 MaybeHandle<Object> exception; |
1222 if (!Execution::TryCall(fun, | 1220 if (!Execution::TryCall(fun, |
(...skipping 1111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2334 return Handle<Object>::null(); | 2332 return Handle<Object>::null(); |
2335 } | 2333 } |
2336 | 2334 |
2337 | 2335 |
2338 Handle<Object> Factory::ToBoolean(bool value) { | 2336 Handle<Object> Factory::ToBoolean(bool value) { |
2339 return value ? true_value() : false_value(); | 2337 return value ? true_value() : false_value(); |
2340 } | 2338 } |
2341 | 2339 |
2342 | 2340 |
2343 } } // namespace v8::internal | 2341 } } // namespace v8::internal |
OLD | NEW |