Skip to content

Commit

Permalink
Remove redundant checks in Object conversion macro
Browse files Browse the repository at this point in the history
Signed-off-by: HyukWoo Park <[email protected]>
  • Loading branch information
clover2123 authored and ksh8281 committed Jan 8, 2024
1 parent 96762b2 commit 1982e20
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 139 deletions.
68 changes: 34 additions & 34 deletions src/builtins/BuiltinArray.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/builtins/BuiltinDataView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static Value builtinDataViewConstructor(ExecutionState& state, Value thisValue,
#define DECLARE_DATAVIEW_GETTER(Name) \
static Value builtinDataViewGet##Name(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
{ \
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, DataView, get##Name); \
Object* thisObject = thisValue.toObject(state); \
if (!(thisObject->isDataViewObject())) { \
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().DataView.string(), \
true, state.context()->staticStrings().get##Name.string(), \
Expand All @@ -109,7 +109,7 @@ static Value builtinDataViewConstructor(ExecutionState& state, Value thisValue,
#define DECLARE_DATAVIEW_SETTER(Name) \
static Value builtinDataViewSet##Name(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
{ \
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, DataView, get##Name); \
Object* thisObject = thisValue.toObject(state); \
if (!(thisObject->isDataViewObject())) { \
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().DataView.string(), \
true, state.context()->staticStrings().set##Name.string(), \
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/BuiltinDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static Value builtinDateToISOString(ExecutionState& state, Value thisValue, size

static Value builtinDateToJSON(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, Date, toJSON);
Object* thisObject = thisValue.toObject(state);

Value tv = Value(thisObject).toPrimitive(state, Value::PreferNumber);
if (tv.isNumber() && (std::isnan(tv.asNumber()) || std::isinf(tv.asNumber()))) {
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/BuiltinNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static Value builtinNumberToString(ExecutionState& state, Value thisValue, size_

static Value builtinNumberToLocaleString(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, Number, toLocaleString);
Object* thisObject = thisValue.toObject(state);
if (!thisObject->isNumberObject()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, ErrorObject::Messages::GlobalObject_ThisNotNumber);
}
Expand Down
9 changes: 4 additions & 5 deletions src/builtins/BuiltinObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static Value builtinObjectConstructor(ExecutionState& state, Value thisValue, si

static Value builtinObjectValueOf(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_OBJECT(ret, Object, valueOf);
return ret;
return thisValue.toObject(state);
}

static Value builtinObjectPreventExtensions(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
Expand Down Expand Up @@ -271,7 +270,7 @@ static Value builtinObjectIsPrototypeOf(ExecutionState& state, Value thisValue,
Value V = argv[0];

// Let O be the result of calling ToObject passing the this value as the argument.
RESOLVE_THIS_BINDING_TO_OBJECT(O, Object, isPrototypeOf);
Object* O = thisValue.toObject(state);

// Repeat
while (true) {
Expand All @@ -293,7 +292,7 @@ static Value builtinObjectPropertyIsEnumerable(ExecutionState& state, Value this
ObjectPropertyName P(state, argv[0]);

// Let O be the result of calling ToObject passing the this value as the argument.
RESOLVE_THIS_BINDING_TO_OBJECT(O, Object, propertyIsEnumerable);
Object* O = thisValue.toObject(state);

// Let desc be the result of calling the [[GetOwnProperty]] internal method of O with argument name.
ObjectGetResult desc = O->getOwnProperty(state, P);
Expand All @@ -311,7 +310,7 @@ static Value builtinObjectToLocaleString(ExecutionState& state, Value thisValue,
// https://www.ecma-international.org/ecma-262/#sec-object.prototype.tolocalestring
// Let O be the this value.
// Return ? Invoke(O, "toString").
RESOLVE_THIS_BINDING_TO_OBJECT(O, Object, toLocaleString);
Object* O = thisValue.toObject(state);
Value toString = O->get(state, ObjectPropertyName(state.context()->staticStrings().toString)).value(state, thisValue);
return Object::call(state, toString, thisValue, 0, nullptr);
}
Expand Down
6 changes: 3 additions & 3 deletions src/builtins/BuiltinRegExp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static Value builtinRegExpConstructor(ExecutionState& state, Value thisValue, si

static Value builtinRegExpExec(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, RegExp, exec);
Object* thisObject = thisValue.toObject(state);
if (!thisObject->isRegExpObject()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().RegExp.string(), true, state.context()->staticStrings().exec.string(), ErrorObject::Messages::GlobalObject_ThisNotRegExpObject);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ static Value regExpExec(ExecutionState& state, Object* R, String* S)

static Value builtinRegExpTest(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_OBJECT(thisObject, RegExp, test);
Object* thisObject = thisValue.toObject(state);
if (!thisObject->isRegExpObject()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().RegExp.string(), true, state.context()->staticStrings().test.string(), ErrorObject::Messages::GlobalObject_ThisNotRegExpObject);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ static Value builtinRegExpCompile(ExecutionState& state, Value thisValue, size_t
static Value builtinRegExpSearch(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
// $21.2.5.9 RegExp.prototype[@@search]
RESOLVE_THIS_BINDING_TO_OBJECT(rx, Object, search);
Object* rx = thisValue.toObject(state);
String* s = argv[0].toString(state);
Value previousLastIndex = rx->get(state, ObjectPropertyName(state.context()->staticStrings().lastIndex)).value(state, thisValue);
if (!previousLastIndex.equalsToByTheSameValueAlgorithm(state, Value(0))) {
Expand Down
6 changes: 6 additions & 0 deletions src/builtins/BuiltinString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ static Value builtinStringToString(ExecutionState& state, Value thisValue, size_
RELEASE_ASSERT_NOT_REACHED();
}

#define RESOLVE_THIS_BINDING_TO_STRING(NAME, OBJ, BUILT_IN_METHOD) \
if (thisValue.isUndefinedOrNull()) { \
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().OBJ.string(), true, state.context()->staticStrings().BUILT_IN_METHOD.string(), ErrorObject::Messages::GlobalObject_ThisUndefinedOrNull); \
} \
String* NAME = thisValue.toString(state);

static Value builtinStringIndexOf(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
RESOLVE_THIS_BINDING_TO_STRING(str, String, indexOf);
Expand Down
Loading

0 comments on commit 1982e20

Please sign in to comment.