-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,55 @@ | ||
import * as t from "@babel/types"; | ||
|
||
export default function () { | ||
return { | ||
visitor: { | ||
ClassDeclaration(path) { | ||
let classNode = path.node | ||
let classNode = path.node; | ||
if (classNode.decorators && classNode.decorators[0].expression.name == 'NearBindgen') { | ||
let classId = classNode.id | ||
let contractMethods = {} | ||
|
||
for(let child of classNode.body.body) { | ||
let classId = classNode.id; | ||
let contractMethods = {}; | ||
for (let child of classNode.body.body) { | ||
if (child.type == 'ClassMethod' && child.kind == 'method' && child.decorators) { | ||
if (child.decorators[0].expression.name == 'call') { | ||
let callMethod = child.key.name | ||
contractMethods[callMethod] = 'call' | ||
} else if (child.decorators[0].expression.name == 'view') { | ||
let viewMethod = child.key.name | ||
contractMethods[viewMethod] = 'view' | ||
let callMethod = child.key.name; | ||
contractMethods[callMethod] = 'call'; | ||
} | ||
else if (child.decorators[0].expression.name == 'view') { | ||
let viewMethod = child.key.name; | ||
contractMethods[viewMethod] = 'view'; | ||
} | ||
else if (child.decorators[0].expression.name == 'initialize') { | ||
let initMethod = child.key.name; | ||
contractMethods[initMethod] = 'initialize'; | ||
} | ||
} | ||
} | ||
|
||
for (let method of Object.keys(contractMethods)) { | ||
path.insertAfter( | ||
t.exportNamedDeclaration( | ||
t.functionDeclaration(t.identifier(method), [], t.blockStatement([ | ||
// let _contract = ContractClass._get() | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_contract'), | ||
t.callExpression(t.memberExpression(classId, t.identifier('_get')), []))]), | ||
// _contract.deserialize() | ||
t.expressionStatement( | ||
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('deserialize')), [])), | ||
// let args = _contract.constructor.deserializeArgs() | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('args'), | ||
t.callExpression(t.memberExpression(t.memberExpression(t.identifier('_contract'), t.identifier('constructor')), t.identifier('deserializeArgs')), []))]), | ||
// let ret = _contract.method(args) | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('ret'), | ||
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier(method)), [t.identifier('args')]))]), | ||
contractMethods[method] == 'call' ? | ||
// _contract.serialize() | ||
t.expressionStatement( | ||
t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('serialize')), [])) | ||
: t.emptyStatement(), | ||
// if (ret !== undefined) | ||
t.ifStatement(t.binaryExpression('!==', t.identifier('ret'), t.identifier('undefined')), | ||
// env.value_return(_contract.constructor.serializeReturn(ret)) | ||
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('env'), t.identifier('value_return')), [ | ||
t.callExpression(t.memberExpression(t.memberExpression(t.identifier('_contract'), t.identifier('constructor')), t.identifier('serializeReturn')), [t.identifier('ret')]) | ||
])) | ||
) | ||
])), | ||
[t.exportSpecifier(t.identifier(method), t.identifier(method))])) | ||
path.scope.registerDeclaration(path.getSibling(path.key + 1)) | ||
path.insertAfter(t.exportNamedDeclaration(t.functionDeclaration(t.identifier(method), [], t.blockStatement([ | ||
// const _state = Counter._getState(); | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_state'), t.callExpression(t.memberExpression(classId, t.identifier('_getState')), []))]), | ||
contractMethods[method] == 'initialize' ? | ||
// if (_state) { throw new Error('Counter already initialized'); } | ||
t.ifStatement(t.identifier('_state'), t.throwStatement(t.newExpression(t.identifier('Error'), [t.stringLiteral('Contract already initialized')]))) | ||
: t.emptyStatement(), | ||
// let _contract = Counter._create(); | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_contract'), t.callExpression(t.memberExpression(classId, t.identifier('_create')), []))]), | ||
// Object.assign(_contract, state); | ||
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('assign')), [t.identifier('_contract'), t.identifier('_state')])), | ||
// let _args = Counter._getArgs(); | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_args'), t.callExpression(t.memberExpression(classId, t.identifier('_getArgs')), []))]), | ||
// let _result = _contract.method(args); | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_result'), t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier(method)), [t.identifier('_args')]))]), | ||
contractMethods[method] == 'initialize' || contractMethods[method] == 'call' ? | ||
// _contract._saveToStorage(); | ||
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('_saveToStorage')), [])) | ||
: t.emptyStatement(), | ||
// if (_result !== undefined) near.valueReturn(_contract._serialize(result)); | ||
t.ifStatement(t.binaryExpression('!==', t.identifier('_result'), t.identifier('undefined')), t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('near'), t.identifier('valueReturn')), [t.callExpression(t.memberExpression(t.identifier('_contract'), t.identifier('_serialize')), [t.identifier('_result')])]))), | ||
])))); | ||
console.log('Near bindgen export done'); | ||
} | ||
|
||
path.insertAfter( | ||
t.exportNamedDeclaration( | ||
t.functionDeclaration(t.identifier('init'), [], t.blockStatement([ | ||
t.expressionStatement(t.callExpression(t.memberExpression(classId, t.identifier('_init')), [])), | ||
])), | ||
[t.exportSpecifier(t.identifier('init'), t.identifier('init'))])) | ||
path.scope.registerDeclaration(path.getSibling(path.key + 1)) | ||
|
||
console.log('Near bindgen export done') | ||
} | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,41 @@ | ||
export function call (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void { | ||
import * as near from "./api"; | ||
|
||
export function initialize(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void { | ||
} | ||
|
||
export function call(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void { | ||
} | ||
|
||
export function view (target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void { | ||
export function view(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<Function>): void { | ||
} | ||
|
||
|
||
export function NearBindgen<T extends { new(...args: any[]): {}}>(target: T) { | ||
export function NearBindgen<T extends { new(...args: any[]): {} }>(target: T) { | ||
return class extends target { | ||
static _init() { | ||
// @ts-ignore | ||
let args = target.deserializeArgs() | ||
let ret = new target(args) | ||
// @ts-ignore | ||
ret.init() | ||
// @ts-ignore | ||
ret.serialize() | ||
return ret | ||
static _create() { | ||
return new target(); | ||
} | ||
|
||
static _getState(): Object { | ||
const rawState = near.storageRead("STATE"); | ||
return rawState ? this._deserialize(rawState) : null; | ||
} | ||
|
||
static _saveToStorage(): void { | ||
near.storageWrite("STATE", this._serialize(this)); | ||
} | ||
|
||
static _getArgs(): JSON { | ||
return JSON.parse(near.input() || "{}"); | ||
} | ||
|
||
static _get() { | ||
let ret = Object.create(target.prototype) | ||
return ret | ||
static _serialize(value: Object): string { | ||
return JSON.stringify(value); | ||
} | ||
|
||
static _deserialize(value: string): Object { | ||
return JSON.parse(value); | ||
} | ||
} | ||
} | ||
|