Skip to content

Commit

Permalink
codegen: basic object destructuring (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobVarioa authored and CanadaHonk committed Jul 10, 2024
1 parent 14b249a commit cd5fe16
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
91 changes: 70 additions & 21 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,7 @@ const generateVar = (scope, decl) => {

let i = 0;
const elements = [...x.id.elements];
// todo: if elements.length == 0 and Porffor.rawType(tmpName) is not iterable, throw a typeerror
for (const e of elements) {
switch (e?.type) {
case 'RestElement': { // let [ ...foo ] = []
Expand Down Expand Up @@ -2937,20 +2938,6 @@ const generateVar = (scope, decl) => {
continue; // skip i++
}

case 'Identifier': { // let [ foo ] = []
decls.push({
type: 'VariableDeclarator',
id: e,
init: {
type: 'MemberExpression',
object: { type: 'Identifier', name: tmpName },
property: { type: 'Literal', value: i }
}
});

break;
}

case 'AssignmentPattern': { // let [ foo = defaultValue ] = []
decls.push({
type: 'VariableDeclarator',
Expand All @@ -2961,7 +2948,8 @@ const generateVar = (scope, decl) => {
left: {
type: 'MemberExpression',
object: { type: 'Identifier', name: tmpName },
property: { type: 'Literal', value: i }
property: { type: 'Literal', value: i },
computed: true
},
right: e.right
}
Expand All @@ -2970,22 +2958,22 @@ const generateVar = (scope, decl) => {
break;
}

case 'ArrayPattern': { // let [ [ foo, bar ] ] = []
case 'ArrayPattern': // let [ [ foo, bar ] ] = []
case 'Identifier': // let [ foo ] = []
case 'ObjectPattern': { // let [ { foo } ] = []
decls.push({
type: 'VariableDeclarator',
id: e,
init: {
type: 'MemberExpression',
object: { type: 'Identifier', name: tmpName },
property: { type: 'Literal', value: i }
property: { type: 'Literal', value: i },
computed: true
}
});

break;
}

case 'ObjectPattern':
return todo(scope, 'object destructuring is not supported yet')
}

i++;
Expand All @@ -3011,8 +2999,69 @@ const generateVar = (scope, decl) => {
continue;
}

if (x.id.type === 'ObjectPattern') {
const decls = [];
const tmpName = '#destructure' + uniqId();

const properties = [...x.id.properties];
// todo: if properties.length == 0 and Porffor.rawType(tmpName) != object, throw a typeerror
for (const prop of properties) {
if (prop.type == 'Property') { // let { foo } = {}
if (prop.value.type === 'AssignmentPattern') { // let { foo = defaultValue } = {}
decls.push({
type: 'VariableDeclarator',
id: prop.value.left,
init: {
type: 'LogicalExpression',
operator: '??',
left: {
type: 'MemberExpression',
object: { type: 'Identifier', name: tmpName },
property: prop.key,
computed: prop.computed
},
right: prop.value.right
}
});
} else {
decls.push({
type: 'VariableDeclarator',
id: prop.value,
init: {
type: 'MemberExpression',
object: { type: 'Identifier', name: tmpName },
property: prop.key,
computed: prop.computed
}
});
}
} else { // let { ...foo } = {}
return todo(scope, 'object rest destructuring is not supported yet')
}
}

out = out.concat([
...generateVar(scope, {
type: 'VariableDeclaration',
declarations: [{
type: 'VariableDeclarator',
id: { type: 'Identifier', name: tmpName },
init: x.init
}],
kind: decl.kind
}),
...generateVar(scope, {
type: 'VariableDeclaration',
declarations: decls,
kind: decl.kind
})
]);

continue;
}

const name = mapName(x.id.name);
if (!name) return todo(scope, 'object destructuring is not supported yet')
if (!name) return todo(scope, `variable declarators of type ${x.id.type} are not supported yet`)

if (x.init && isFuncType(x.init.type)) {
// hack for let a = function () { ... }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "porffor",
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
"version": "0.28.2+b2c217063",
"version": "0.28.3+a17fb257e",
"author": "CanadaHonk",
"license": "MIT",
"scripts": {},
Expand Down
2 changes: 1 addition & 1 deletion runner/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'node:fs';
globalThis.version = '0.28.2+b2c217063';
globalThis.version = '0.28.3+a17fb257e';

// deno compat
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
Expand Down

0 comments on commit cd5fe16

Please sign in to comment.