Skip to content

Commit

Permalink
Add object expression
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e committed Aug 14, 2019
1 parent 7ae8ff5 commit de70bb7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
11 changes: 3 additions & 8 deletions jsc/src/play.ts
Expand Up @@ -11,14 +11,9 @@ import { dump } from "./dump";

function main() {
const source = `
function x() {
var ch = 0;
state.lastStringValue = "";
while (isUnicodePropertyNameCharacter(ch = state.current())) {
state.lastStringValue += codePointToString(ch);
state.advance();
}
return state.lastStringValue !== ""
var a = {
x: 5,
[p]: 1
}
`;

Expand Down
39 changes: 33 additions & 6 deletions jsc/src/visitor.ts
Expand Up @@ -542,6 +542,16 @@ export function visit(writer: Writer, node: estree.Node): void {
break;
}

case "ReturnStatement": {
if (node.argument) {
visit(writer, node.argument);
} else {
writer.write(ByteCode.LdUndef);
}
writer.write(ByteCode.Ret);
break;
}

case "ArrayExpression": {
writer.write(ByteCode.LdArr);

Expand All @@ -557,13 +567,30 @@ export function visit(writer: Writer, node: estree.Node): void {
break;
}

case "ReturnStatement": {
if (node.argument) {
visit(writer, node.argument);
} else {
writer.write(ByteCode.LdUndef);
case "ObjectExpression": {
writer.write(ByteCode.LdObj);
for (const property of node.properties) {
if (property.type === ("SpreadElement" as any))
throw new Error("Spread obj element is not implemented yet.");

if (property.kind !== "init")
throw new Error("Getter/Setter is not supported yet.");

// TODO(qti3e) ComputedRef and PropRef pop the object from the ds and
// they're not suited to be used here.
if (property.computed) {
visit(writer, property.key);
writer.write(ByteCode.ComputedRef);
} else {
writer.write(
ByteCode.PropRef,
(property.key as estree.Identifier).name
);
}

visit(writer, property.value);
writer.write(ByteCode.Asgn);
}
writer.write(ByteCode.Ret);
break;
}

Expand Down

0 comments on commit de70bb7

Please sign in to comment.