Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new way of create object in script #1303

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/builtins/BuiltinTemporal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ static Value builtinTemporalPlainDateTimeConstructor(ExecutionState& state, Valu

ASSERT(argc > 2);

if (!(argv[0].isInteger(state) || argv[1].isInteger(state) || argv[2].isInteger(state)) || !(argc > 3 && argv[3].isInteger(state)) || !(argc > 4 && argv[4].isInteger(state)) || !(argc > 5 && argv[5].isInteger(state)) || !(argc > 6 && argv[6].isInteger(state)) || !(argc > 7 && argv[7].isInteger(state)) || !(argc > 8 && argv[8].isInteger(state))) {
if (!argv[0].isInteger(state) || !argv[1].isInteger(state) || !argv[2].isInteger(state)
|| (argc > 3 && !argv[3].isInteger(state)) || (argc > 4 && !argv[4].isInteger(state))
|| (argc > 5 && !argv[5].isInteger(state)) || (argc > 6 && !argv[6].isInteger(state))
|| (argc > 7 && !argv[7].isInteger(state)) || (argc > 8 && !argv[8].isInteger(state))) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, "Invalid type");
}

Expand Down Expand Up @@ -802,14 +805,14 @@ static Value builtinTemporalDurationConstructor(ExecutionState& state, Value thi
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, ErrorObject::Messages::New_Target_Is_Undefined);
}

int values[10];
double values[10];
memset(values, 0, sizeof(values));

for (unsigned int i = 0; i < argc && i < 10; ++i) {
if (!argv[i].isInteger(state)) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, "Invalid type");
}
values[i] = argv[i].asInt32();
values[i] = argv[i].toInteger(state);
}

return TemporalDurationObject::createTemporalDuration(state, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], newTarget);
Expand Down
89 changes: 87 additions & 2 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct GlobalVariableAccessCacheItem;
F(BinaryUnsignedRightShift) \
F(BinaryInOperation) \
F(BinaryInstanceOfOperation) \
F(CreateObjectPrepare) \
F(CreateObject) \
F(CreateArray) \
F(CreateSpreadArrayObject) \
Expand Down Expand Up @@ -953,20 +954,104 @@ class BreakpointEnabled : public ByteCode {
COMPILE_ASSERT(sizeof(BreakpointDisabled) == sizeof(BreakpointEnabled), "");
#endif /* ESCARGOT_DEBUGGER */

class CreateObjectPrepare : public ByteCode {
public:
enum Stage ENSURE_ENUM_UNSIGNED {
Init,
FillKeyValue,
DefineGetterSetter,
};

struct CreateObjectData : public gc {
ObjectStructureItemVector m_properties;
EncodedValueVector m_values;
Object* m_target;
CreateObjectData(size_t reserveSize, Object* target)
: m_target(target)
{
m_properties.reserve(reserveSize);
m_values.reserve(reserveSize);
}
};

CreateObjectPrepare(const ByteCodeLOC& loc, const size_t dataRegisterIndex, const size_t objectIndex)
: ByteCode(Opcode::CreateObjectPrepareOpcode, loc)
, m_stage(Stage::Init)
, m_hasPreComputedKey(false)
, m_needsToUpdateFunctionName(false)
, m_isGetter(false)
, m_dataRegisterIndex(dataRegisterIndex)
, m_propertyReserveSize(0)
, m_objectIndex(objectIndex)
{
}

CreateObjectPrepare(const ByteCodeLOC& loc, const bool hasPreComputedKey, const size_t dataRegisterIndex, const size_t keyIndex, const size_t valueIndex, const bool needsToUpdateFunctionName)
: ByteCode(Opcode::CreateObjectPrepareOpcode, loc)
, m_stage(Stage::FillKeyValue)
, m_hasPreComputedKey(hasPreComputedKey)
, m_needsToUpdateFunctionName(needsToUpdateFunctionName)
, m_isGetter(false)
, m_dataRegisterIndex(dataRegisterIndex)
, m_keyIndex(keyIndex)
, m_valueIndex(valueIndex)
{
}

CreateObjectPrepare(const ByteCodeLOC& loc, const bool hasPreComputedKey, const bool isGetter, const size_t dataRegisterIndex, const size_t keyIndex, const size_t valueIndex)
: ByteCode(Opcode::CreateObjectPrepareOpcode, loc)
, m_stage(Stage::DefineGetterSetter)
, m_hasPreComputedKey(hasPreComputedKey)
, m_needsToUpdateFunctionName(false)
, m_isGetter(isGetter)
, m_dataRegisterIndex(dataRegisterIndex)
, m_keyIndex(keyIndex)
, m_valueIndex(valueIndex)
{
}

Stage m_stage : 2;
bool m_hasPreComputedKey : 1;
bool m_needsToUpdateFunctionName : 1;
bool m_isGetter : 1; // other case, this is setter
ByteCodeRegisterIndex m_dataRegisterIndex : REGISTER_INDEX_IN_BIT;

union {
struct {
ByteCodeRegisterIndex m_propertyReserveSize : REGISTER_INDEX_IN_BIT;
ByteCodeRegisterIndex m_objectIndex : REGISTER_INDEX_IN_BIT;
};

struct {
ByteCodeRegisterIndex m_keyIndex : REGISTER_INDEX_IN_BIT;
ByteCodeRegisterIndex m_valueIndex : REGISTER_INDEX_IN_BIT;
};
};

#ifndef NDEBUG
void dump()
{
printf("createobjectprepare -> r%u stage: %d", m_dataRegisterIndex, m_stage);
}
#endif
};

class CreateObject : public ByteCode {
public:
CreateObject(const ByteCodeLOC& loc, const size_t registerIndex)
CreateObject(const ByteCodeLOC& loc, const size_t registerIndex, const size_t dataRegisterIndex = SIZE_MAX)
: ByteCode(Opcode::CreateObjectOpcode, loc)
, m_registerIndex(registerIndex)
, m_dataRegisterIndex(dataRegisterIndex)
{
}

ByteCodeRegisterIndex m_registerIndex;
ByteCodeRegisterIndex m_dataRegisterIndex;

#ifndef NDEBUG
void dump()
{
printf("createobject -> r%u", m_registerIndex);
printf("createobject -> r%u r%u", m_registerIndex, m_dataRegisterIndex);
}
#endif
};
Expand Down
10 changes: 10 additions & 0 deletions src/interpreter/ByteCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ void ByteCodeGenerator::relocateByteCode(ByteCodeBlock* block)
case CreateObjectOpcode: {
CreateObject* cd = (CreateObject*)currentCode;
ASSIGN_STACKINDEX_IF_NEEDED(cd->m_registerIndex, stackBase, stackBaseWillBe, stackVariableSize);
ASSIGN_STACKINDEX_IF_NEEDED(cd->m_dataRegisterIndex, stackBase, stackBaseWillBe, stackVariableSize);
break;
}
case CreateObjectPrepareOpcode: {
CreateObjectPrepare* cd = (CreateObjectPrepare*)currentCode;
ASSIGN_STACKINDEX_IF_NEEDED(cd->m_dataRegisterIndex, stackBase, stackBaseWillBe, stackVariableSize);
if (cd->m_stage == CreateObjectPrepare::FillKeyValue || cd->m_stage == CreateObjectPrepare::DefineGetterSetter) {
ASSIGN_STACKINDEX_IF_NEEDED(cd->m_keyIndex, stackBase, stackBaseWillBe, stackVariableSize);
ASSIGN_STACKINDEX_IF_NEEDED(cd->m_valueIndex, stackBase, stackBaseWillBe, stackVariableSize);
}
break;
}
case CreateArrayOpcode: {
Expand Down
Loading