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

Commit

Permalink
feat: support class property
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 6, 2018
1 parent 01c2856 commit afbef33
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 145 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ It base on [https://github.com/bramblex/jsjs](https://github.com/bramblex/jsjs)
* [ ] RegExp Unicode Property Escapes (Depends on environment)
* [ ] Experimental
* [x] Object rest spread
* [x] Class property

## Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

| [<img src="https://avatars1.githubusercontent.com/u/9758711?v=3" width="100px;"/><br /><sub>Axetroy</sub>](http://axetroy.github.io)<br />[💻](https://github.com/axetroy/vm.js/commits?author=axetroy) 🔌 [⚠️](https://github.com/axetroy/vm.js/commits?author=axetroy) [🐛](https://github.com/axetroy/vm.js/issues?q=author%3Aaxetroy) 🎨 |
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |


<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down
7 changes: 4 additions & 3 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ const evaluate_map = {
function Class(...args) {
_classCallCheck(this, Class);

// TODO: need babel plugin to support class property
const newScope = scope.$child("function");

// babel way to call super();
Expand All @@ -706,7 +705,9 @@ const evaluate_map = {
newScope.$const("this", __this);

// define class property
properties.forEach(p => (__this[p.key.name] = p.value));
properties.forEach(p => {
__this[p.key.name] = evaluate(p.value, newScope);
});

if (constructor) {
// defined the params
Expand All @@ -728,7 +729,7 @@ const evaluate_map = {
.map((method: types.ClassMethod) => {
const newScope = scope.$child("function");
const func = function(...args) {
newScope.$const("this", this);
newScope.$var("this", this);

// defined the params
method.params.forEach((p: types.LVal, i) => {
Expand Down
282 changes: 141 additions & 141 deletions test/ClassDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,147 @@ import * as fs from "fs";

import vm from "../src/vm";

// test("ClassDeclaration-contructor", t => {
// const sandbox: any = vm.createContext({});

// const People: any = vm.runInContext(
// `
// class People{
// constructor(name){
// this.name = name;
// this.word = "hello " + name;
// }
// }

// module.exports = People;
// `,
// sandbox
// );

// const people = new People("axetroy");

// t.deepEqual(people.name, "axetroy");
// t.deepEqual(people.word, "hello axetroy");
// t.true(people instanceof People);
// t.true(people.__proto__.constructor === People);
// });

// // test("ClassDeclaration-property", t => {
// // const sandbox: any = vm.createContext({});

// // const People: any = vm.runInContext(
// // `
// // class People{
// // age = 21;
// // constructor(name){
// // this.name = name;
// // }
// // getAge(){
// // return this.age;
// // }
// // }

// // module.exports = People;
// // `,
// // sandbox
// // );

// // const people = new People("axetroy");

// // t.deepEqual(people.name, "axetroy");
// // t.deepEqual(people.age, 21);
// // t.deepEqual(people.getAge(), 21);
// // t.true(people instanceof People);
// // });

// test("ClassDeclaration-method", t => {
// const sandbox: any = vm.createContext({});

// const Base: any = vm.runInContext(
// `
// class Base{
// constructor(){
// this.name = "hello world";
// }
// say(word){
// return "hello " + word;
// }
// }

// module.exports = Base;
// `,
// sandbox
// );

// const base = new Base();

// t.deepEqual(base.name, "hello world");
// t.true(base instanceof Base);
// t.deepEqual(base.say("world"), "hello world");
// });

// test("ClassDeclaration-getter and setter", t => {
// const sandbox: any = vm.createContext({});

// const People: any = vm.runInContext(
// `
// class People{
// constructor(name, age){
// this.name = name;
// this.__age = age;
// }
// get age(){
// return this.__age;
// }
// set age(age){
// this.__age = age;
// }
// }

// module.exports = People;
// `,
// sandbox
// );

// const people = new People("axetroy", 21);

// t.deepEqual(people.name, "axetroy");
// t.deepEqual(people.__age, 21);
// t.deepEqual(people.age, 21);
// t.true(people instanceof People);

// people.age = 22;
// t.deepEqual(people.__age, 22);
// t.deepEqual(people.age, 22);
// });

// test("ClassDeclaration-extends", t => {
// const sandbox: any = vm.createContext({});

// const {Life, People} = vm.runInContext(
// `
// class Life{
// eat(){

// }
// }
// class People extends Life{
// learn(){

// }
// }

// module.exports = {
// Life,
// People
// };
// `,
// sandbox
// );
// const people = new People("axetroy");
// t.deepEqual(typeof people.eat, "function"); // inherti from Life
// t.deepEqual(typeof people.learn, "function");
// });
test("ClassDeclaration-contructor", t => {
const sandbox: any = vm.createContext({});

const People: any = vm.runInContext(
`
class People{
constructor(name){
this.name = name;
this.word = "hello " + name;
}
}
module.exports = People;
`,
sandbox
);

const people = new People("axetroy");

t.deepEqual(people.name, "axetroy");
t.deepEqual(people.word, "hello axetroy");
t.true(people instanceof People);
t.true(people.__proto__.constructor === People);
});

test("ClassDeclaration-property", t => {
const sandbox: any = vm.createContext({});

const People: any = vm.runInContext(
`
class People{
age = 21;
constructor(name){
this.name = name;
}
getAge(){
return this.age;
}
}
module.exports = People;
`,
sandbox
);

const people = new People("axetroy");

t.deepEqual(people.name, "axetroy");
t.deepEqual(people.age, 21);
t.deepEqual(people.getAge(), 21);
t.true(people instanceof People);
});

test("ClassDeclaration-method", t => {
const sandbox: any = vm.createContext({});

const Base: any = vm.runInContext(
`
class Base{
constructor(){
this.name = "hello world";
}
say(word){
return "hello " + word;
}
}
module.exports = Base;
`,
sandbox
);

const base = new Base();

t.deepEqual(base.name, "hello world");
t.true(base instanceof Base);
t.deepEqual(base.say("world"), "hello world");
});

test("ClassDeclaration-getter and setter", t => {
const sandbox: any = vm.createContext({});

const People: any = vm.runInContext(
`
class People{
constructor(name, age){
this.name = name;
this.__age = age;
}
get age(){
return this.__age;
}
set age(age){
this.__age = age;
}
}
module.exports = People;
`,
sandbox
);

const people = new People("axetroy", 21);

t.deepEqual(people.name, "axetroy");
t.deepEqual(people.__age, 21);
t.deepEqual(people.age, 21);
t.true(people instanceof People);

people.age = 22;
t.deepEqual(people.__age, 22);
t.deepEqual(people.age, 22);
});

test("ClassDeclaration-extends", t => {
const sandbox: any = vm.createContext({});

const {Life, People} = vm.runInContext(
`
class Life{
eat(){
}
}
class People extends Life{
learn(){
}
}
module.exports = {
Life,
People
};
`,
sandbox
);
const people = new People("axetroy");
t.deepEqual(typeof people.eat, "function"); // inherti from Life
t.deepEqual(typeof people.learn, "function");
});

test("ClassDeclaration-extends and super", t => {
const sandbox: any = vm.createContext({});
Expand Down

0 comments on commit afbef33

Please sign in to comment.