Skip to content

Commit

Permalink
Fix pre-processor spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
jafri committed Mar 7, 2022
1 parent 546691f commit 8a3fd87
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 115 deletions.
8 changes: 4 additions & 4 deletions as-packages/chain/assembly/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export function internal(_: string): any {
export function other(_: string): any {
//
}
export function contract(_: string): any {
export function contract(_: any): any {
//
}
export function action(_: string, __?: any): any {
export function action(_: any, __?: any): any {
//
}
export function ignore(_: string): any {
Expand All @@ -17,7 +17,7 @@ export function packed(_: string): any {
//
}

export function table(_: string, __: string=""): any {
export function table(_: any, __: string=""): any {
//
}

Expand All @@ -30,7 +30,7 @@ export function serializer(_: string): any {
export function primary(_: any, __: any): any {
//
}
export function secondary(_: any, __: any): any {
export function secondary(_: any, __?: any): any {
//
}
export function packer(_: any): any {
Expand Down
5 changes: 3 additions & 2 deletions ts-packages/transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"build": "rm -rf dist/* && tsc -p tsconfig.json"
},
"bin": {
"eosio-asc": "bin/.bin/eosio-asc"
"eosio-asc": "bin/.bin/eosio-asc"
},
"dependencies": {
"assemblyscript": "0.18",
"handlebars": "4.7.7"
"handlebars": "4.7.7",
"ts-dedent": "^2.2.0"
}
}
146 changes: 71 additions & 75 deletions ts-packages/transform/src/preprocess/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { TypeHelper } from "../utils/typeutil";
import { TableInterpreter } from "../contract/classdef";
import { RangeUtil } from "../utils/utils";
import { NamedTypeNodeDef } from "../contract/typedef";

import {
Range,
} from "assemblyscript";
import { Range } from "assemblyscript";
import dedent from "ts-dedent"

const WIN = process.platform === "win32";
const EOL = WIN ? "\r\n" : "\n";
Expand All @@ -37,14 +35,14 @@ Handlebars.registerHelper("generateActionMember", function (fn: ParameterNodeDef
let code: string[] = [];
let plainType = fn.type.plainTypeNode;
if (plainType == 'string') {
code.push(` ${fn.name}: string = "";`);
code.push(`${fn.name}: string = "";`);
} else if (plainType == 'string[]') {
code.push(` ${fn.name}: string[] = [];`);
code.push(`${fn.name}: string[] = [];`);
} else {
if (TypeHelper.isPrimitiveType(fn.type.typeKind)) {
code.push(` ${fn.name}: ${plainType};`);
code.push(`${fn.name}: ${plainType};`);
} else {
code.push(` ${fn.name}!: ${plainType};`);
code.push(`${fn.name}!: ${plainType};`);
}
}
return code.join(EOL);
Expand All @@ -55,14 +53,14 @@ Handlebars.registerHelper("generateActionParam", function (fn: ParameterNodeDef)
let plainType = fn.type.plainTypeNode;

if (plainType == 'string') {
code.push(` ${fn.name}: string = "",`);
code.push(`${fn.name}: string = "",`);
} else if (plainType == 'string[]') {
code.push(` ${fn.name}: string[] = [],`);
code.push(`${fn.name}: string[] = [],`);
} else {
if (TypeHelper.isPrimitiveType(fn.type.typeKind)) {
code.push(` ${fn.name}: ${plainType} = 0,`);
code.push(`${fn.name}: ${plainType} = 0,`);
} else {
code.push(` ${fn.name}: ${plainType} | null = null,`);
code.push(`${fn.name}: ${plainType} | null = null,`);
}
}

Expand All @@ -71,7 +69,7 @@ Handlebars.registerHelper("generateActionParam", function (fn: ParameterNodeDef)

Handlebars.registerHelper("generateActionConstructor", function (fn: ParameterNodeDef) {
let code: string[] = [];
code.push(` if(${fn.name}) this.${fn.name} = ${fn.name};`);
code.push(`if(${fn.name}) this.${fn.name} = ${fn.name};`);
return code.join(EOL);
});

Expand All @@ -86,25 +84,25 @@ function fieldSerialize(name: string, type: NamedTypeNodeDef) {
}
let numType = numberTypeMap.get(plainType.replace('[]', ''));
if (numType) {
code.push(` enc.packNumberArray<${numType}>(this.${name})`);
code.push(`enc.packNumberArray<${numType}>(this.${name})`);
} else if (plainType == 'string') {
code.push(` enc.packStringArray(this.${name})`);
code.push(`enc.packStringArray(this.${name})`);
} else {
code.push(` enc.packObjectArray(this.${name});`);
code.push(`enc.packObjectArray(this.${name});`);
}
} else if (type.typeKind == TypeKindEnum.MAP) {
throw Error(`map type is not supported currently!Trace ${RangeUtil.location(type.typeNode.range)}`);
} else {
let plainType = type.plainTypeNode;
let numType = numberTypeMap.get(plainType);
if (numType) {
code.push(` enc.packNumber<${numType}>(this.${name});`);
code.push(`enc.packNumber<${numType}>(this.${name});`);
} else if (plainType == 'boolean') {
code.push(` enc.packNumber<u8>(<u8>this.${name});`);
code.push(`enc.packNumber<u8>(<u8>this.${name});`);
} else if (plainType == 'string') {
code.push(` enc.packString(this.${name});`);
code.push(`enc.packString(this.${name});`);
} else {
code.push(` enc.pack(this.${name});`);
code.push(`enc.pack(this.${name});`);
}
}
return code.join(EOL);
Expand All @@ -121,34 +119,36 @@ function fieldDeserialize(name: string, type: NamedTypeNodeDef) {
}
let numType = numberTypeMap.get(plainType);
if (numType) {
code.push(` this.${name} = dec.unpackNumberArray<${numType}>();`);
code.push(`this.${name} = dec.unpackNumberArray<${numType}>();`);
} else if (plainType == 'string' ) {
code.push(` this.${name} = dec.unpackStringArray();`);
code.push(`this.${name} = dec.unpackStringArray();`);
} else {
code.push(`{
let length = <i32>dec.unpackLength();
this.${name} = new Array<${plainType}>(length)
for (let i=0; i<length; i++) {
let obj = new ${plainType}();
this.${name}[i] = obj;
dec.unpack(obj);
}
}`);
code.push(dedent`
{
let length = <i32>dec.unpackLength();
this.${name} = new Array<${plainType}>(length)
for (let i=0; i<length; i++) {
let obj = new ${plainType}();
this.${name}[i] = obj;
dec.unpack(obj);
}
}
`);
}
} else if (type.typeKind == TypeKindEnum.MAP) {
throw Error(`map is not supported currently!Trace: ${RangeUtil.location(type.typeNode.range)}`);
} else {
let plainType = type.plainTypeNode;
let numType = numberTypeMap.get(plainType);
if (numType) {
code.push(` this.${name} = dec.unpackNumber<${numType}>();`);
code.push(`this.${name} = dec.unpackNumber<${numType}>();`);
} else if (plainType == 'boolean') {
code.push(` this.${name} = <boolean>dec.unpackNumber<u8>();`);
code.push(`this.${name} = <boolean>dec.unpackNumber<u8>();`);
} else if (plainType == 'string') {
code.push(` this.${name} = dec.unpackString();`);
code.push(`this.${name} = dec.unpackString();`);
} else {
code.push(` this.${name} = new ${plainType}();`);
code.push(` dec.unpack(this.${name});`);
code.push(`this.${name} = new ${plainType}();`);
code.push(` dec.unpack(this.${name});`);
}
}
return code.join(EOL);
Expand All @@ -157,7 +157,7 @@ function fieldDeserialize(name: string, type: NamedTypeNodeDef) {
function fieldGetSize(name: string, type: NamedTypeNodeDef) {
let code: string[] = [];
if (type.typeKind == TypeKindEnum.ARRAY) {
code.push(` size += _chain.calcPackedVarUint32Length(this.${name}.length);`);
code.push(`size += _chain.calcPackedVarUint32Length(this.${name}.length);`);
let plainType = type.plainTypeNode;

if (plainType.indexOf('Array<') >= 0) {
Expand All @@ -168,31 +168,31 @@ function fieldGetSize(name: string, type: NamedTypeNodeDef) {

let numType = numberTypeMap.get(plainType);
if (numType) {
code.push(` size += sizeof<${plainType}>()*this.${name}.length;`);
code.push(` size += sizeof<${plainType}>()*this.${name}.length;`);
} else if (plainType == 'string') {
code.push(`
for (let i=0; i<this.${name}.length; i++) {
size += _chain.Utils.calcPackedStringLength(this.${name}[i]);
}
`);
code.push(dedent`\n
for (let i=0; i<this.${name}.length; i++) {
size += _chain.Utils.calcPackedStringLength(this.${name}[i]);
}
\n`);
} else {
code.push(`
for (let i=0; i<this.${name}.length; i++) {
size += this.${name}[i].getSize();
}
`);
code.push(dedent`\n
for (let i=0; i<this.${name}.length; i++) {
size += this.${name}[i].getSize();
}
\n`);
}
} else if (type.typeKind == TypeKindEnum.MAP) {
throw Error(`map type is not supported currently!Trace ${RangeUtil.location(type.typeNode.range)}`);
} else {
let plainType = type.plainTypeNode;
let numType = numberTypeMap.get(plainType);
if (numType) {
code.push(` size += sizeof<${numType}>();`);
code.push(`size += sizeof<${numType}>();`);
} else if (plainType == 'string') {
code.push(` size += _chain.Utils.calcPackedStringLength(this.${name});`);
code.push(`size += _chain.Utils.calcPackedStringLength(this.${name});`);
} else {
code.push(` size += this.${name}.getSize();`);
code.push(`size += this.${name}.getSize();`);
}
}
return code.join(EOL);
Expand Down Expand Up @@ -414,20 +414,20 @@ Handlebars.registerHelper("actionParameterGetSize", function (field: ParameterNo
});

function handleAction(action: ActionFunctionDef): string {
let code: string[] = [];

let parameters: string[] = [];
action.parameters.forEach(parameter => {
parameters.push(`args.${parameter.name}`, );
});
let actionName = action.messageDecorator.actionName;
let actionNameHex = EosioUtils.nameToHexString(actionName);
code.push(`if (action == ${actionNameHex}) {//${actionName}`);
code.push(` let args = new ${action.methodName}Action();`);
code.push(` args.unpack(actionData);`);
code.push(` mycontract.${action.methodName}(${parameters.join(',')})`);
code.push(` }`);
return code.join(EOL);

return dedent`
if (action == ${actionNameHex}) {//${actionName}
let args = new ${action.methodName}Action();
args.unpack(actionData);
mycontract.${action.methodName}(${parameters.join(',')})
}
`;
}

Handlebars.registerHelper("handleAction", function (action: ActionFunctionDef) {
Expand Down Expand Up @@ -460,21 +460,17 @@ Handlebars.registerHelper("ExtractClassBody", function (range: Range) {
Handlebars.registerHelper("generategetPrimaryFunction", function (table: TableInterpreter) {
let code: string[] = [];
if (table.singleton) {
code.push(
`
getPrimaryValue(): u64 {
return _chain.Name.fromString("${table.tableName}").N;
}
`
);
code.push(dedent`
getPrimaryValue(): u64 {
return _chain.Name.fromString("${table.tableName}").N;
}
`);
} else {
code.push(
`
code.push(dedent`
getPrimaryValue(): u64 {
return this.${table.primaryFuncDef!.getterPrototype!.declaration.name.text}
}
`
);
return this.${table.primaryFuncDef!.getterPrototype!.declaration.name.text}
}
`);
}
return code.join(EOL);
});
Expand Down Expand Up @@ -555,10 +551,10 @@ Handlebars.registerHelper("generateGetIdxDBFunction", function (fn: DBIndexFunct
plainType = plainType.toUpperCase();
let dbClass = dbTypeToDBClass.get(plainType);

code.push(`
code.push(dedent`
get ${fn.getterPrototype!.declaration.name.text}DB(): _chain.${dbClass} {
return <_chain.${dbClass}>this.idxdbs[${fn._index}];
}
return <_chain.${dbClass}>this.idxdbs[${fn._index}];
}
`);
return code.join(EOL);
});
Expand Down
30 changes: 14 additions & 16 deletions ts-packages/transform/src/tpl/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import { CONFIG } from "../config/compile";
export const mainTpl = `
export function apply(receiver: u64, firstReceiver: u64, action: u64): void {
let _receiver = new _chain.Name(receiver);
let _firstReceiver = new _chain.Name(firstReceiver);
let _action = new _chain.Name(action);
let _receiver = new _chain.Name(receiver);
let _firstReceiver = new _chain.Name(firstReceiver);
let _action = new _chain.Name(action);
let mycontract = new {{contract.className}}(_receiver, _firstReceiver, _action);
let actionData = _chain.readActionData();
let mycontract = new {{contract.className}}(_receiver, _firstReceiver, _action);
let actionData = _chain.readActionData();
if (receiver == firstReceiver) {
{{#each contract.actionFuncDefs}}
{{{handleAction .}}}
{{/each}}
}
if (receiver == firstReceiver) {
{{#each contract.actionFuncDefs}}
{{{handleAction .}}}
{{/each}}
}
if (receiver != firstReceiver) {
{{#each contract.actionFuncDefs}}
{{{handleNotifyAction .}}}
{{/each}}
}
return;
if (receiver != firstReceiver) {
{{#each contract.actionFuncDefs}}{{{handleNotifyAction .}}}{{/each}}
}
return;
}
`;

Expand Down
Loading

0 comments on commit 8a3fd87

Please sign in to comment.