Skip to content

Commit

Permalink
Don't generate code if class implements Packer or MultiIndexValue
Browse files Browse the repository at this point in the history
  • Loading branch information
learnforpractice committed Apr 27, 2022
1 parent a9d255e commit 95d8bff
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
2 changes: 2 additions & 0 deletions as-packages/chain/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"build:optional": "npx eosio-asc testoptional.ts --target release",
"build:extension": "npx eosio-asc testbinaryextension.ts --target release",
"build:apply": "npx eosio-asc testapply.ts --target release",
"build:nocodegen": "npx eosio-asc testnocodegen.ts --target release",
"test-all": "yarn build && run-ipyeos -m pytest -s -x test.py",
"test:name": "yarn build:name && run-ipyeos -m pytest -s -x test.py -k test_name",
"test:publickey": "yarn build:publickey && run-ipyeos -m pytest -s -x test.py -k test_publickey",
Expand All @@ -57,6 +58,7 @@
"test:extension": "yarn build:extension && run-ipyeos -m pytest -s -x test.py -k test_binaryextension",
"test:contract": "yarn build:contract && run-ipyeos -m pytest -s -x test.py -k test_contract",
"test:apply": "yarn build:apply && run-ipyeos -m pytest -s -x test.py -k test_apply",
"test:nocodegen": "yarn build:nocodegen && run-ipyeos -m pytest -s -x test.py -k test_nocodegen",
"test:transform": "npx eosio-asc gencode testtransform.ts -o ./target"
},
"dependencies": {
Expand Down
15 changes: 14 additions & 1 deletion as-packages/chain/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,17 @@ def test_apply():
}
r = chain.push_action('hello', 'sayhello', args, {'hello': 'active'})
logger.info('++++++elapsed: %s', r['elapsed'])
chain.produce_block()
chain.produce_block()

@chain_test
def test_nocodegen():
code, abi = get_code_and_abi('testnocodegen')
chain.deploy_contract('hello', code, abi, 0)

args = {
"a1": {"a": 123},
"a2": {"aaa": 123, "bbb": 456},
}
r = chain.push_action('hello', 'testnogen', args, {'hello': 'active'})
logger.info('++++++elapsed: %s', r['elapsed'])
chain.produce_block()
105 changes: 105 additions & 0 deletions as-packages/chain/tests/testnocodegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
Asset,
Encoder,
Decoder,
Packer,
Contract,
} from "as-chain";

import * as _chain from "as-chain";

class MyData implements Packer {

public a: u64
pack(): u8[] {
let enc = new Encoder(this.getSize());
enc.packNumber<u64>(this.a);
return enc.getBytes();
}

unpack(data: u8[]): usize {
let dec = new Decoder(data);
this.a = dec.unpackNumber<u64>();
return dec.getPos();
}

getSize(): usize {
let size: usize = 0;
size += sizeof<u64>();
return size;
}
}

class MyTableDB extends _chain.MultiIndex<MyTable> {

}

@table("hello", "singleton")
class MyTable implements _chain.MultiIndexValue {

aaa: u64;
bbb: u64;
constructor(
aaa: u64 = 0,
bbb: u64 = 0
){
this.aaa = aaa;
this.bbb = bbb;
}

pack(): u8[] {
let enc = new _chain.Encoder(this.getSize());
enc.packNumber<u64>(this.aaa);
enc.packNumber<u64>(this.bbb);
return enc.getBytes();
}

unpack(data: u8[]): usize {
let dec = new _chain.Decoder(data);
this.aaa = dec.unpackNumber<u64>();
this.bbb = dec.unpackNumber<u64>();
return dec.getPos();
}

getSize(): usize {
let size: usize = 0;
size += sizeof<u64>();
size += sizeof<u64>();
return size;
}

getPrimaryValue(): u64 {
return _chain.Name.fromU64(0x6AA31A0000000000).N;
}

getSecondaryValue(i: i32): _chain.SecondaryValue {
switch (i) {
default:
_chain.assert(false, "bad db index!");
return new _chain.SecondaryValue(_chain.SecondaryType.U64, new Array<u64>(0));
}
}

setSecondaryValue(i: i32, value: _chain.SecondaryValue): void {
switch (i) {
default:
_chain.assert(false, "bad db index!");
}
}

static new(code: _chain.Name, scope: _chain.Name): _chain.Singleton<MyTable> {
let tableName = _chain.Name.fromU64(0x6AA31A0000000000);
return new _chain.Singleton<MyTable>(code, scope, tableName);
}
}

@contract
class MyContract extends Contract {
@action("testnogen")
testNoGenCode(
a1: MyData,
a2: MyTable,
): void {

}
}
9 changes: 7 additions & 2 deletions ts-packages/transform/src/ascoption.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ var APIOptionImpl = /** @class */ (function () {
text_1 = modifySourceText(text_1, item);
});
let importLang = `import * as _chain from "as-chain";\n`;
if (text_1.indexOf(importLang) < 0) {
text_1 = importLang + text_1;
}

// console.log("+++++++process.libPaths:", process.libPaths);
importLang = "";
if (process.libPaths && text_1.indexOf("apply(") >= 0) {
process.libPaths.forEach((value, key) => {
importLang += `import * as ${value} from '${key}';\n`
Expand Down Expand Up @@ -155,7 +158,9 @@ var APIOptionImpl = /** @class */ (function () {
text_1 = modifySourceText(text_1, item);
});
let importLang = `import * as _chain from "as-chain";\n`;
text_1 = importLang + text_1;
if (text_1.indexOf(importLang) < 0) {
text_1 = importLang + text_1;
}
text_1 = optimizeCode(text_1);
fs.writeFileSync(outputFile, text_1);
}
Expand Down
11 changes: 11 additions & 0 deletions ts-packages/transform/src/contract/classdef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export class ClassInterpreter {
ContractDecoratorKind.VARIANT,
];

if (this.classPrototype.interfacePrototypes) {
let hasPackerInterface = this.classPrototype.interfacePrototypes.find(x => {
return x.internalName == "~lib/as-chain/serializer/Packer" ||
x.internalName == "~lib/as-chain/mi/MultiIndexValue";
});

if (hasPackerInterface) {
this.no_codegen = true;
}
}

for (let i=0; i<classTypes.length; i++) {
let kind = classTypes[i];
this.decorator = AstUtil.getSpecifyDecorator(clzPrototype.declaration, kind);
Expand Down

0 comments on commit 95d8bff

Please sign in to comment.