2222#include " Context.h"
2323#include " SandBox.h"
2424#include " NativeFunctionObject.h"
25+ #ifdef ENABLE_EXTENDED_API
26+ #include " VMInstance.h"
27+ #endif
2528
2629namespace Escargot {
2730
@@ -158,22 +161,39 @@ static Value builtinErrorObjectStackInfoSet(ExecutionState& state, Value thisVal
158161 return Value ();
159162}
160163
161- ErrorObject::ErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
164+ ErrorObject::ErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
162165 : DerivedObject(state, proto)
163166 , m_stackTraceData(nullptr )
164167{
168+ UNUSED_PARAMETER (triggerCallback);
169+ Context* context = state.context ();
170+
165171 if (errorMessage->length ()) {
166172 defineOwnPropertyThrowsException (state, state.context ()->staticStrings ().message ,
167173 ObjectPropertyDescriptor (errorMessage, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent)));
168174 }
169175
170- Context* context = state.context ();
176+ #if defined(ENABLE_EXTENDED_API)
177+ if (UNLIKELY (triggerCallback)) {
178+ ASSERT (context->vmInstance ()->isErrorCreationCallbackRegistered ());
179+ // trigger ErrorCreationCallback
180+ context->vmInstance ()->triggerErrorCreationCallback (state, this );
181+ if (this ->hasOwnProperty (state, ObjectPropertyName (context->staticStrings ().stack ))) {
182+ // if ErrorCreationCallback is registered and this callback already inserts `stack` property for the created ErrorObject,
183+ // we just ignore adding `stack` data here
184+ return ;
185+ }
186+ }
187+ #endif
188+
171189 JSGetterSetter gs (
172190 new NativeFunctionObject (state, NativeFunctionInfo (context->staticStrings ().stack , builtinErrorObjectStackInfoGet, 0 , NativeFunctionInfo::Strict)),
173191 new NativeFunctionObject (state, NativeFunctionInfo (context->staticStrings ().stack , builtinErrorObjectStackInfoSet, 0 , NativeFunctionInfo::Strict)));
174192 ObjectPropertyDescriptor desc (gs, ObjectPropertyDescriptor::ConfigurablePresent);
175193 defineOwnProperty (state, ObjectPropertyName (context->staticStrings ().stack ), desc);
194+
176195 if (fillStackInfo) {
196+ // fill stack info at the creation of Error object rather than the moment of thrown
177197 updateStackTraceData (state);
178198 }
179199}
@@ -185,54 +205,54 @@ void ErrorObject::updateStackTraceData(ExecutionState& state)
185205 setStackTraceData (StackTraceData::create (stackTraceDataVector));
186206}
187207
188- ReferenceErrorObject::ReferenceErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
189- : ErrorObject(state, proto, errorMessage, fillStackInfo)
208+ ReferenceErrorObject::ReferenceErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
209+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
190210{
191211}
192212
193- TypeErrorObject::TypeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
194- : ErrorObject(state, proto, errorMessage, fillStackInfo)
213+ TypeErrorObject::TypeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
214+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
195215{
196216}
197217
198- RangeErrorObject::RangeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
199- : ErrorObject(state, proto, errorMessage, fillStackInfo)
218+ RangeErrorObject::RangeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
219+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
200220{
201221}
202222
203- SyntaxErrorObject::SyntaxErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
204- : ErrorObject(state, proto, errorMessage, fillStackInfo)
223+ SyntaxErrorObject::SyntaxErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
224+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
205225{
206226}
207227
208- URIErrorObject::URIErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
209- : ErrorObject(state, proto, errorMessage, fillStackInfo)
228+ URIErrorObject::URIErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
229+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
210230{
211231}
212232
213- EvalErrorObject::EvalErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
214- : ErrorObject(state, proto, errorMessage, fillStackInfo)
233+ EvalErrorObject::EvalErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
234+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
215235{
216236}
217237
218- AggregateErrorObject::AggregateErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
219- : ErrorObject(state, proto, errorMessage, fillStackInfo)
238+ AggregateErrorObject::AggregateErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
239+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
220240{
221241}
222242
223243#if defined(ENABLE_WASM)
224- WASMCompileErrorObject::WASMCompileErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
225- : ErrorObject(state, proto, errorMessage, fillStackInfo)
244+ WASMCompileErrorObject::WASMCompileErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
245+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
226246{
227247}
228248
229- WASMLinkErrorObject::WASMLinkErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
230- : ErrorObject(state, proto, errorMessage, fillStackInfo)
249+ WASMLinkErrorObject::WASMLinkErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
250+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
231251{
232252}
233253
234- WASMRuntimeErrorObject::WASMRuntimeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo)
235- : ErrorObject(state, proto, errorMessage, fillStackInfo)
254+ WASMRuntimeErrorObject::WASMRuntimeErrorObject (ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo, bool triggerCallback )
255+ : ErrorObject(state, proto, errorMessage, fillStackInfo, triggerCallback )
236256{
237257}
238258#endif
0 commit comments