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

[Design] add copy constructor of List and Map (BREAKING) #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 31 additions & 7 deletions src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,17 +931,41 @@ static void _ctorString(PKVM* vm) {
}

static void _ctorList(PKVM* vm) {
List* list = newList(vm, ARGC);
vmPushTempRef(vm, &list->_super); // list.
for (int i = 0; i < ARGC; i++) {
listAppend(vm, list, ARG(i + 1));
if (!pkCheckArgcRange(vm, ARGC, 0, 1)) return;
List* list;
int64_t natural;

if (ARGC == 1) {
if (isInteger(ARG(1), &natural) && natural >= 0) {
list = newList(vm, natural);
list->elements.count = natural;

} else if (IS_OBJ_TYPE(ARG(1), OBJ_LIST)) {
List* src_list = (List*) AS_OBJ(ARG(1));
list = listAdd(vm, src_list, NULL);

} else {
RET_ERR(newString(vm, "Expected a natural number or a list."));
}
} else {
list = newList(vm, 0);
}
vmPopTempRef(vm); // list.
RET(VAR_OBJ(list));
}

static void _ctorMap(PKVM* vm) {
RET(VAR_OBJ(newMap(vm)));
if (!pkCheckArgcRange(vm, ARGC, 0, 1)) return;
Map* map;

if (ARGC == 1) {
Map* src_map;
if (!validateArgMap(vm, 1, &src_map)) return;
map = mapDup(vm, src_map);

} else {
map = newMap(vm);
}
RET(VAR_OBJ(map));
}

static void _ctorRange(PKVM* vm) {
Expand Down Expand Up @@ -1432,7 +1456,7 @@ static void initializePrimitiveClasses(PKVM* vm) {
ADD_CTOR(PK_STRING, "@ctorString", _ctorString, -1);
ADD_CTOR(PK_RANGE, "@ctorRange", _ctorRange, 2);
ADD_CTOR(PK_LIST, "@ctorList", _ctorList, -1);
ADD_CTOR(PK_MAP, "@ctorMap", _ctorMap, 0);
ADD_CTOR(PK_MAP, "@ctorMap", _ctorMap, -1);
ADD_CTOR(PK_FIBER, "@ctorFiber", _ctorFiber, 1);
#undef ADD_CTOR

Expand Down
23 changes: 16 additions & 7 deletions src/core/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,17 +908,15 @@ void listClear(PKVM* vm, List* self) {
}

List* listAdd(PKVM* vm, List* l1, List* l2) {
// l1 and l2 can be NULL, and always return a new list
// returned list should not have the same reference of l1 or l2.

// Optimize end case.
if (l1->elements.count == 0) return l2;
if (l2->elements.count == 0) return l1;

uint32_t size = l1->elements.count + l2->elements.count;
uint32_t size = (l1? l1->elements.count: 0) + (l2? l2->elements.count: 0);
List* list = newList(vm, size);

vmPushTempRef(vm, &list->_super); // list.
pkVarBufferConcat(&list->elements, vm, &l1->elements);
pkVarBufferConcat(&list->elements, vm, &l2->elements);
if (l1) pkVarBufferConcat(&list->elements, vm, &l1->elements);
if (l2) pkVarBufferConcat(&list->elements, vm, &l2->elements);
vmPopTempRef(vm); // list.

return list;
Expand Down Expand Up @@ -1126,6 +1124,17 @@ Var mapRemoveKey(PKVM* vm, Map* self, Var key) {
return value;
}

Map* mapDup(PKVM* vm, Map* self) {
Map* map = newMap(vm);
vmPushTempRef(vm, &map->_super); // map.
map->capacity = self->capacity;
map->count = self->count;
map->entries = ALLOCATE_ARRAY(vm, MapEntry, self->capacity);
memcpy(map->entries, self->entries, self->capacity * sizeof(MapEntry));
vmPopTempRef(vm); // map
return map;
}

bool fiberHasError(Fiber* fiber) {
return fiber->error != NULL;
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ void mapClear(PKVM* vm, Map* self);
// otherwise return VAR_UNDEFINED.
Var mapRemoveKey(PKVM* vm, Map* self, Var key);

// Duplicate a map.
Map* mapDup(PKVM* vm, Map* self);

// Returns true if the fiber has error, and if it has any the fiber cannot be
// resumed anymore.
bool fiberHasError(Fiber* fiber);
Expand Down