Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
feat: support generator function
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 9, 2018
1 parent f2a6ef6 commit 4cf7143
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ It base on [https://github.com/bramblex/jsjs](https://github.com/bramblex/jsjs)
* [x] template literals
* [x] Lifting template literal restriction
* [ ] unicode-regex
* [ ] generator
* [x] Generator function
* [ ] es2016
* [x] exponentiation operator
* [ ] es2017
Expand Down
66 changes: 58 additions & 8 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
_possibleConstructorReturn,
_inherits,
_toConsumableArray,
_taggedTemplateLiteral
_taggedTemplateLiteral,
__generator
} from "./runtime";
import {Path} from "./path";
import {EvaluateMap, EvaluateFunc} from "./type";
Expand Down Expand Up @@ -144,6 +145,7 @@ const evaluate_map: EvaluateMap = {
if (!scope.$declar(kind, name, value)) {
throw new ErrDuplicateDeclard(name);
}
return value;
} else if (types.isObjectPattern(declartor.id)) {
// @es2015 destrucuring
const vars: {key: string; alias: string}[] = [];
Expand Down Expand Up @@ -175,8 +177,11 @@ const evaluate_map: EvaluateMap = {
const el = declartor.init.elements[i];
if (!el) {
scope.$declar(kind, $varName, undefined);
return undefined;
} else {
scope.$declar(kind, $varName, evaluate(path.$child(el)));
const result = evaluate(path.$child(el));
scope.$declar(kind, $varName, result);
return result;
}
} else {
throw node;
Expand All @@ -200,23 +205,65 @@ const evaluate_map: EvaluateMap = {
if (types.isObjectProperty(n)) {
const propertyName: string = (<any>n).id.name;
const $var = newScope.$find(propertyName);
scope.$var(propertyName, $var ? $var.$get() : undefined);
const varValue = $var ? $var.$get() : undefined;
scope.$var(propertyName, varValue);
return varValue;
}
});
} else if (types.isObjectExpression(node.init)) {
const varName: string = (<types.Identifier>node.id).name;
scope.$var(varName, evaluate(path.$child(node.init)));
const varValue = evaluate(path.$child(node.init));
scope.$var(varName, varValue);
return varValue;
} else {
throw node;
}
},
FunctionDeclaration(path) {
const {node, scope} = path;
const {name: func_name} = node.id;
if (node.async === true) {
} else if (node.generator) {
const generatorFunc = function generatorFunc() {
const __this = this;

function handler(_a) {
const functionBody = node.body;
const block = functionBody.body[_a.label];
// the last block
if (!block) {
return [2, undefined];
}

const fieldContext = {
call: false,
value: null
};
function next(value) {
fieldContext.value = value;
fieldContext.call = true;
_a.sent();
}

const r = evaluate(path.$child(block, path.scope, {next: next}));
if (types.isReturnStatement(block)) {
return [2, r.result];
}
if (fieldContext.call) {
return [4, fieldContext.value];
} else {
// next block
_a.label++;
return handler(_a);
}
}

return __generator(__this, handler);
};
// function declartion can be duplicate
scope.$var(func_name, generatorFunc);
} else {
const func = evaluate_map.FunctionExpression(path.$child(<any>node));
const {name: func_name} = node.id;

Object.defineProperties(func, {
length: {
value: node.params.length
Expand Down Expand Up @@ -572,7 +619,7 @@ const evaluate_map: EvaluateMap = {
},
FunctionExpression(path) {
const {node, scope} = path;
const func = function(..._arguments) {
const func = function functionDeclaration(..._arguments) {
const newScope = scope.$child("function");
newScope.invasived = true;
for (let i = 0; i < node.params.length; i++) {
Expand Down Expand Up @@ -1091,7 +1138,10 @@ const evaluate_map: EvaluateMap = {
const {value} = ctx;
scope.$const((<types.Identifier>node.argument).name, value);
},
YieldExpression(path) {},
YieldExpression(path) {
const {next} = path.ctx;
next(evaluate(path.$child(path.node.argument))); // call next
},
SequenceExpression(path) {},
TaggedTemplateExpression(path) {
const string = path.node.quasi.quasis.map(v => v.value.cooked);
Expand Down
28 changes: 28 additions & 0 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ export function _asyncToGenerator(fn) {
};
}

export const __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { (<any>_) = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; (<any[]>_.ops).push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};

export function _taggedTemplateLiteral(strings: string[], raw: string[]) {
return Object.freeze(
Object.defineProperties(strings, {raw: {value: Object.freeze(raw)}})
Expand Down
2 changes: 1 addition & 1 deletion src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Vm {
});

const path = new Path(ast, null, scope, {});

ast && evaluate(path);

// exports
Expand Down
101 changes: 101 additions & 0 deletions test/es2015/generator/GeneratorFunctionExpression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import test from "ava";
import vm from "../../../src/vm";

test("GeneratoeFunction-1", t => {
const sandbox: any = vm.createContext({
name: "world"
});

const get = vm.runInContext(
`
function* get(){
var a = 123;
yield a;
}
module.exports = get;
`,
sandbox
);

const generator = get();
t.deepEqual(generator.next(), {done: false, value: 123});
t.deepEqual(generator.next(), {done: true, value: undefined});
});

test("GeneratoeFunction-2", t => {
const sandbox: any = vm.createContext({
name: "world"
});

const get = vm.runInContext(
`
function* get(){
var a = 123;
yield a;
var b = "hello world";
yield b;
}
module.exports = get;
`,
sandbox
);

const generator = get();
t.deepEqual(generator.next(), {done: false, value: 123});
t.deepEqual(generator.next(), {done: false, value: "hello world"});
t.deepEqual(generator.next(), {done: true, value: undefined});
});

test("GeneratoeFunction-3", t => {
const sandbox: any = vm.createContext({
name: "world"
});

const get = vm.runInContext(
`
function* get(){
var a = 123;
yield a;
var b = "hello world";
yield b;
return 233;
}
module.exports = get;
`,
sandbox
);

const generator = get();
t.deepEqual(generator.next(), {done: false, value: 123});
t.deepEqual(generator.next(), {done: false, value: "hello world"});
t.deepEqual(generator.next(), {done: true, value: 233});
});

test("GeneratoeFunction-4", t => {
const sandbox: any = vm.createContext({
name: "world"
});

const get = vm.runInContext(
`
function* get(){
var a = 123;
yield a;
var b = "hello world";
var c = yield b;
return c;
}
module.exports = get;
`,
sandbox
);

const generator = get();
t.deepEqual(generator.next(), {done: false, value: 123});
t.deepEqual(generator.next(), {done: false, value: "hello world"});
t.deepEqual(generator.next(), {done: true, value: undefined});
});

0 comments on commit 4cf7143

Please sign in to comment.