Skip to content

Commit 57c23d6

Browse files
clover2123ksh8281
authored andcommitted
Fix Error creation process to check and trigger Error relevant callbacks correctly
Signed-off-by: HyukWoo Park <[email protected]>
1 parent 77f0c49 commit 57c23d6

File tree

4 files changed

+65
-50
lines changed

4 files changed

+65
-50
lines changed

src/builtins/BuiltinError.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
5252
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* {
5353
return constructorRealm->globalObject()->errorPrototype();
5454
});
55+
56+
#if defined(ENABLE_EXTENDED_API)
57+
ErrorObject* obj = new ErrorObject(state, proto, String::emptyString, true, state.context()->vmInstance()->isErrorCreationCallbackRegistered());
58+
#else
5559
ErrorObject* obj = new ErrorObject(state, proto, String::emptyString);
60+
#endif
5661

5762
Value message = argv[0];
5863
if (!message.isUndefined()) {
@@ -63,12 +68,6 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
6368
Value options = argc > 1 ? argv[1] : Value();
6469
installErrorCause(state, obj, options);
6570

66-
#if defined(ENABLE_EXTENDED_API)
67-
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
68-
state.context()->vmInstance()->triggerErrorCreationCallback(state, obj);
69-
}
70-
#endif
71-
7271
return obj;
7372
}
7473

@@ -82,17 +81,14 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
8281
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* { \
8382
return constructorRealm->globalObject()->lowerCaseErrorName##ErrorPrototype(); \
8483
}); \
85-
ErrorObject* obj = new errorName##ErrorObject(state, proto, String::emptyString); \
84+
ErrorObject* obj = new errorName##ErrorObject(state, proto, String::emptyString, true, state.context()->vmInstance()->isErrorCreationCallbackRegistered()); \
8685
Value message = argv[0]; \
8786
if (!message.isUndefined()) { \
8887
obj->defineOwnPropertyThrowsException(state, state.context()->staticStrings().message, \
8988
ObjectPropertyDescriptor(message.toString(state), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent))); \
9089
} \
9190
Value options = argc > 1 ? argv[1] : Value(); \
9291
installErrorCause(state, obj, options); \
93-
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) { \
94-
state.context()->vmInstance()->triggerErrorCreationCallback(state, obj); \
95-
} \
9692
return obj; \
9793
}
9894
#else
@@ -134,7 +130,13 @@ static Value builtinAggregateErrorConstructor(ExecutionState& state, Value thisV
134130
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* {
135131
return constructorRealm->globalObject()->aggregateErrorPrototype();
136132
});
133+
134+
#if defined(ENABLE_EXTENDED_API)
135+
ErrorObject* O = new AggregateErrorObject(state, proto, String::emptyString, true, state.context()->vmInstance()->isErrorCreationCallbackRegistered());
136+
#else
137137
ErrorObject* O = new AggregateErrorObject(state, proto, String::emptyString);
138+
#endif
139+
138140
Value message = argv[1];
139141
// If message is not undefined, then
140142
if (!message.isUndefined()) {
@@ -155,12 +157,6 @@ static Value builtinAggregateErrorConstructor(ExecutionState& state, Value thisV
155157
O->defineOwnPropertyThrowsException(state, ObjectPropertyName(state, String::fromASCII("errors")),
156158
ObjectPropertyDescriptor(Value(Object::createArrayFromList(state, errorsList.size(), errorsList.data())), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent)));
157159

158-
#if defined(ENABLE_EXTENDED_API)
159-
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
160-
state.context()->vmInstance()->triggerErrorCreationCallback(state, O);
161-
}
162-
#endif
163-
164160
// Return O.
165161
return O;
166162
}

src/runtime/ErrorObject.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "Context.h"
2323
#include "SandBox.h"
2424
#include "NativeFunctionObject.h"
25+
#ifdef ENABLE_EXTENDED_API
26+
#include "VMInstance.h"
27+
#endif
2528

2629
namespace 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

src/runtime/ErrorObject.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class ErrorObject : public DerivedObject {
171171
static void throwBuiltinError(ExecutionState& state, ErrorCode code, String* objectName, bool prototype, String* functionName, const char* templateString);
172172
static void throwBuiltinError(ExecutionState& state, ErrorCode code, String* errorMessage);
173173

174-
ErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
174+
ErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
175175
void updateStackTraceData(ExecutionState& state);
176176

177177
virtual bool isErrorObject() const
@@ -195,53 +195,53 @@ class ErrorObject : public DerivedObject {
195195

196196
class ReferenceErrorObject : public ErrorObject {
197197
public:
198-
ReferenceErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
198+
ReferenceErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
199199
};
200200

201201
class TypeErrorObject : public ErrorObject {
202202
public:
203-
TypeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
203+
TypeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
204204
};
205205

206206
class SyntaxErrorObject : public ErrorObject {
207207
public:
208-
SyntaxErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
208+
SyntaxErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
209209
};
210210

211211
class RangeErrorObject : public ErrorObject {
212212
public:
213-
RangeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
213+
RangeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
214214
};
215215

216216
class URIErrorObject : public ErrorObject {
217217
public:
218-
URIErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
218+
URIErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
219219
};
220220

221221
class EvalErrorObject : public ErrorObject {
222222
public:
223-
EvalErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
223+
EvalErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
224224
};
225225

226226
class AggregateErrorObject : public ErrorObject {
227227
public:
228-
AggregateErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
228+
AggregateErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
229229
};
230230

231231
#if defined(ENABLE_WASM)
232232
class WASMCompileErrorObject : public ErrorObject {
233233
public:
234-
WASMCompileErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
234+
WASMCompileErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
235235
};
236236

237237
class WASMLinkErrorObject : public ErrorObject {
238238
public:
239-
WASMLinkErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
239+
WASMLinkErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
240240
};
241241

242242
class WASMRuntimeErrorObject : public ErrorObject {
243243
public:
244-
WASMRuntimeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true);
244+
WASMRuntimeErrorObject(ExecutionState& state, Object* proto, String* errorMessage, bool fillStackInfo = true, bool triggerCallback = false);
245245
};
246246
#endif
247247
} // namespace Escargot

src/runtime/ObjectStructure.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ ObjectStructure* ObjectStructure::create(Context* ctx, ObjectStructureItemTightV
5656
bool hasSymbol = false;
5757
bool hasNonAtomicPropertyName = false;
5858
bool hasEnumerableProperty = true;
59-
bool isInlineCacheable = true;
6059

6160
for (size_t i = 0; i < properties.size(); i++) {
6261
const ObjectStructurePropertyName& propertyName = properties[i].m_propertyName;

0 commit comments

Comments
 (0)