Skip to content

Commit

Permalink
hello,world
Browse files Browse the repository at this point in the history
  • Loading branch information
learnforpractice committed Feb 3, 2022
1 parent 39d110e commit b9deab4
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Logs
__pycache__
.DS_Store
logs
*.log
Expand Down
1 change: 1 addition & 0 deletions tests/counter/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../../ts-packages/transform/bin/.bin/ask ./counter.ts --use abort= --runtime stub --target release
34 changes: 34 additions & 0 deletions tests/counter/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@storage
class Stored {
value: u32;
}

@contract
class Incrementer {
private stored: Stored;

constructor() {
chain.sayHello();
this.stored = new Stored();
}

@constructor
default(initValue: u32): void {
//this.stored.value = initValue;
}

@message
inc(): void {
// let v = this.stored.value;
// this.stored.value = ++v;
}

@message(mutates = false)
get(): u32 {
return this.stored.value;
}

apply(receiver: u64, firstReceiver: u64, action: u64): void {
chain.sayHello();
}
}
21 changes: 21 additions & 0 deletions tests/counter/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import sys
import json

test_dir = os.path.dirname(__file__)
sys.path.append(os.path.join(test_dir, '..'))

from ipyeos import log
from ipyeos.chaintester import ChainTester

logger = log.get_logger(__name__)

chain = ChainTester()

def test_ts():
# info = chain.get_account('helloworld11')
# logger.info(info)
with open('./target/target.wasm', 'rb') as f:
code = f.read()
chain.deploy_contract('hello', code, b'', 0)
r = chain.push_action('hello', 'test', b'goodbye,world', {'hello': 'active'})
1 change: 1 addition & 0 deletions tests/counter/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run-ipyeos -m pytest -s -x test.py -k test_ts
11 changes: 6 additions & 5 deletions ts-packages/transform/bin/.bin/ask
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const asc = module.exports = require("ask-transform/src/asc/asc.js");
// const asc = module.exports = require("assemblyscript/cli/asc.js");
const path = require("path");
const ARGS = [
"--importMemory",
// "--importMemory",
"--initialMemory",
"2",
"--maximumMemory",
"16",
"--noExportMemory",
"1",
// "--maximumMemory",
// "16",
// "--noExportMemory",
"--runtime",
"stub",
"--use",
"abort= ",
"-O2",
Expand Down
3 changes: 2 additions & 1 deletion ts-packages/transform/src/ascoption.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var APIOptionImpl = /** @class */ (function () {
}).forEach(function (item) {
text_1 = modifySourceText(text_1, item);
});
let importLang = `import * as ${CONFIG.module} from "ask-lang";\n`;
// let importLang1 = `import * as ${CONFIG.module} from "ask-lang";\n`;
let importLang = `import * as chain from "as-chain";\n`;
text_1 = importLang + text_1;
sourceModifier.fileExtension.set(filename, text_1);
// console.log(`The file ${filename} extension: ${text_1}`);
Expand Down
3 changes: 3 additions & 0 deletions ts-packages/transform/src/ascoption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from "path";
import * as fs from "fs";
export class APIOptionImpl implements APIOptions {
modifySourceText(sourceText: string, point: ModifyPoint): string {
console.log("++++++++++ascoption.ts:modifySourceText:", point.code);
if (point.mode == ModifyType.REPLACE) {
let prefix = sourceText.substring(0, point.range.start);
let suffix = sourceText.substring(point.range.end, sourceText.length);
Expand All @@ -15,13 +16,15 @@ export class APIOptionImpl implements APIOptions {
}

readFile(filename: string, baseDir: string) : string | null {
console.log("++++++++++ascoption.ts:readFile:", filename, baseDir);
let name = path.resolve(baseDir, filename);
try {
let text = fs.readFileSync(name, "utf8");
let sourceModifier = process.sourceModifier ? process.sourceModifier : new SourceModifier();
if (sourceModifier.fileExtMap.has(filename)) {
let extCodes = sourceModifier.fileExtMap.get(filename);
extCodes!.sort((a: ModifyPoint, b: ModifyPoint) => (b.range.end - a.range.end)).forEach(item => {
console.log("++++++point.code:", item.code)
text = this.modifySourceText(text, item);
});
}
Expand Down
4 changes: 2 additions & 2 deletions ts-packages/transform/src/config/compile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

export const CONFIG = {
language: "ask v0.1",
compiler: "ask v0.1, asc v0.18",
language: "eosio-asc v0.1",
compiler: "eosio-asc v0.1, asc v0.18",
scope: "_lang.",
module: "_lang",
metadataVersion: "1.0"
Expand Down
2 changes: 2 additions & 0 deletions ts-packages/transform/src/contract/classdef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ export class ClassInterpreter {
}

resolveFieldMembers(): void {
console.log("+++++this.className:", this.className)
this.classPrototype.instanceMembers &&
this.classPrototype.instanceMembers.forEach((element, _) => {
if (element.kind == ElementKind.FIELD_PROTOTYPE) {
console.log("++++++++element.field:", element.name)
this.fields.push(new FieldDef(<FieldPrototype>element));
}
});
Expand Down
1 change: 1 addition & 0 deletions ts-packages/transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class AskTransform extends Transform {
out.entryDir = baseDir;
process.sourceModifier = out;
const abiPath = path.join("target", "metadata.json");
console.log("++++++writeFile:", abiPath)
this.writeFile(abiPath, abi, baseDir);
}
}
25 changes: 25 additions & 0 deletions ts-packages/transform/src/preprocess/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Handlebars.registerHelper("storeGetter", function (field: FieldDef) {
code.push(` if (this.${field.varName} === null) {`);
code.push(` this.${field.varName} = new ${field.type.instanceType}("${field.selector.key}");`);
} else {
console.log("+++++++field.type.plainType:", field.type.plainType);
code.push(`get ${field.name}(): ${field.type.plainType} {`);
code.push(` if (this.${field.varName} === null) {`);
if (field.decorators.isIgnore) {
Expand All @@ -160,6 +161,30 @@ Handlebars.registerHelper("storeGetter", function (field: FieldDef) {
return code.join(EOL);
});

Handlebars.registerHelper("serialize", function (field: FieldDef) {
let code: string[] = [];
if (field.type.typeKind == TypeKindEnum.ARRAY) {
} else if (field.type.typeKind == TypeKindEnum.MAP) {
} else {
if (field.type.plainType == 'u32') {
code.push(`enc.packU32(this.${field.name})`)
}
}
return code.join(EOL);
});

Handlebars.registerHelper("deserialize", function (field: FieldDef) {
let code: string[] = [];
if (field.type.typeKind == TypeKindEnum.ARRAY) {
} else if (field.type.typeKind == TypeKindEnum.MAP) {
} else {
if (field.type.plainType == 'u32') {
code.push(`this.${field.name} = dec.unpackU32()`)
}
}
return code.join(EOL);
});

/**
* set {{name}}(v: {{type.plainType}}) {
this.{{varName}} = new {{type.codecTypeAlias}}(v);
Expand Down
46 changes: 4 additions & 42 deletions ts-packages/transform/src/tpl/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,9 @@ import { CONFIG } from "../config/compile";
let scope = CONFIG.scope;

export const mainTpl = `
export function deploy(): i32 {
export function apply(receiver: u64, firstReceiver: u64, action: u64): void {
let {{contract.instanceName}} = new {{contract.className}}();
{{#each contract.cntrFuncDefs}}
const {{methodName}}Selector: u8[] = {{#selector methodName}}{{shortArr}}{{/selector}};
if (${scope}msg.isSelector({{methodName}}Selector)) {
{{#neq parameters.length 0}}
const fnParameters = new ${scope}FnParameters(${scope}msg.data);
{{/neq}}
{{#each parameters}}
let p{{_index}} = fnParameters.get<{{type.codecTypeAlias}}>();
{{/each}}
{{../contract.instanceName}}.{{methodName}}({{#joinParams parameters}}{{/joinParams}}{{ctrDefaultVals}});
}
{{/each}}
return 0;
{{contract.instanceName}}.apply(receiver, firstReceiver, action);
return;
}
export function call(): i32 {
const {{contract.instanceName}} = new {{contract.className}}();
{{#each contract.msgFuncDefs}}
const {{methodName}}Selector: u8[] = {{selector.shortArr}};
if (${scope}msg.isSelector({{methodName}}Selector)) {
{{#eq messageDecorator.mutates 'false'}}
${scope}Storage.mode = ${scope}StoreMode.R;
{{/eq}}
{{#neq parameters.length 0}}
const fnParameters = new ${scope}FnParameters(${scope}msg.data);
{{/neq}}
{{#each parameters}}
let p{{_index}} = fnParameters.get<{{type.codecTypeAlias}}>();
{{/each}}
{{#if isReturnable}}
let rs = {{../contract.instanceName}}.{{methodName}}({{#joinParams parameters}}{{/joinParams}});
${scope}ReturnData.set<{{returnType.codecTypeAlias}}>({{{wrapResult returnType}}});
{{/if}}
{{#unless isReturnable}}
{{../contract.instanceName}}.{{methodName}}({{#joinParams parameters}}{{/joinParams}});
{{/unless}}
}
{{/each}}
return 0;
}`;
`;
22 changes: 16 additions & 6 deletions ts-packages/transform/src/tpl/store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
export const storeTpl = `{{export}}class {{className}} {
export const storeTpl = `{{export}}class {{className}} extends chain.Serializer {
{{#each fields}}
private {{varName}}: {{{type.plainTypeNode}}} | null = null;
private {{name}}: {{{type.plainType}}};
{{/each}}
{{#each fields}}
{{{storeGetter .}}}
{{{storeSetter .}}}
{{/each}}
serialize(): u8[] {
let enc = new chain.Encoder(10);
{{#each fields}}
{{{serialize .}}}
{{/each}}
return enc.getBytes();
}
deserialize(data: u8[]): void {
let dec = new chain.Decoder(data);
{{#each fields}}
{{{deserialize .}}}
{{/each}}
}
}`;
Loading

0 comments on commit b9deab4

Please sign in to comment.