Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make u128 a special case #51

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions bindings/assembly/nearEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { storage, near, base64, base58, logging } from 'near-runtime-ts';
import { JSONEncoder } from "assemblyscript-json";
import { JSONDecoder, ThrowingJSONHandler, DecoderState } from "assemblyscript-json";
import { u128 } from "bignum";
// Runtime functions
// tslint:disable: no-unsafe-any

Expand Down Expand Up @@ -63,9 +64,13 @@ function encode<T>(encoder: JSONEncoder, value: T, name: string | null = ""): JS
}
encoder.popArray();
}
} else {
//@ts-ignore
value.encode(encoder, name);
} else { // Is an object
if (value instanceof u128){
encoder.setString(name, value.toString());
} else {
//@ts-ignore
value.encode(encoder, name);
}
}
} else {
throw new Error("Encoding failed");
Expand Down Expand Up @@ -143,6 +148,35 @@ class PrimitiveHandler<T> extends ThrowingJSONHandler {

}

function decodeString(buffer: Uint8Array, state: DecoderState | null ): string {
return PrimitiveHandler.String.decode(buffer, state);
}
function decodeInt<T>(buffer: Uint8Array, state: DecoderState | null ): T {
let val: T;
//@ts-ignore
if (val instanceof u32) {
//@ts-ignore
return PrimitiveHandler.U32.decode(buffer, state);
}
//@ts-ignore
if (val instanceof i32) {
//@ts-ignore
return PrimitiveHandler.I32.decode(buffer, state);
}
//@ts-ignore
if (val instanceof u64) {
//@ts-ignore
return PrimitiveHandler.U64.decode(buffer, state);
}
//@ts-ignore
if (val instanceof i64) {
//@ts-ignore
return PrimitiveHandler.I64.decode(buffer, state);
}
//@ts-ignore
return val;
}

@global
class ArrayHandler<T> extends ThrowingJSONHandler {
firstArrayPush: boolean = true;
Expand Down Expand Up @@ -182,6 +216,7 @@ class ArrayHandler<T> extends ThrowingJSONHandler {
this.value.push(<T>U64.parseInt(value));
return;
}
//@ts-ignore
if (item instanceof i64) {
//@ts-ignore
this.value.push(<T>I64.parseInt(value));
Expand Down Expand Up @@ -260,7 +295,7 @@ function decode<T>(buffer: Uint8Array, state: DecoderState | null = null): T {
}
if (isInteger<T>()) {
//@ts-ignore
return <T>PrimitiveHandler.U64.decode(buffer, state);
return decodeInt<T>(buffer, state);
}
assert(isReference<T>(), "type must be an integer, boolean, string, object, or array");
var value: T;
Expand All @@ -272,6 +307,11 @@ function decode<T>(buffer: Uint8Array, state: DecoderState | null = null): T {
}
return ArrayHandler.decode<T>(buffer, state);
} else {
//@ts-ignore
if (value instanceof u128) {
//@ts-ignore
return u128.fromString(decodeString(buffer, state));
}
let value = instantiate<T>();
//@ts-ignore
return value.decode(buffer, state);
Expand Down
2 changes: 1 addition & 1 deletion bindings/dist/transformerBundle.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions bindings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function isReference(type: TypeNode): boolean {
"i64",
"u64",
"Uint8Array",
"boolean"
"boolean",
"u128"
];
return !simpleTypes.includes(toString(type));
}
Expand Down Expand Up @@ -218,7 +219,7 @@ export { __wrapper_${name} as ${name} }
valuePrefix,
"String",
"string",
fieldsWithTypes(["string", "i64", "u64", "Uint8Array"])
fieldsWithTypes(["string", "i64", "u64", "Uint8Array", "u128"])
);
this.generateBasicSetterHandlers(
valuePrefix,
Expand Down Expand Up @@ -299,6 +300,12 @@ export { __wrapper_${name} as ${name} }
` if (name == "${field.name}") {
${valuePrefix}${field.name} = base64.decode(value);
return;
}`);
} else if (fieldTypeName === "u128") {
this.sb.push(
` if (name == "${field.name}") {
${valuePrefix}${field.name} = u128.fromString(value);
return;
}`);
} else {
let className = this.typeName(field.type) === "u64" ? "U64" : "I64";
Expand Down
2 changes: 1 addition & 1 deletion dist/asc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/asc.js.map

Large diffs are not rendered by default.

270 changes: 0 additions & 270 deletions tests/near-bindgen/assembly/index.ts

This file was deleted.

Loading