Comparing version
@@ -1,1 +0,1 @@ | ||
export { JSON } from "./json"; | ||
export { JSON } from "./src/json"; |
@@ -1,1 +0,1 @@ | ||
export { JSON } from "./assembly/json"; | ||
export { JSON } from "./assembly/src/json"; |
{ | ||
"name": "json-as", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -32,3 +32,4 @@ "types": "assembly/index.ts", | ||
"dependencies": { | ||
"as-string-sink": "^0.5.0" | ||
"as-string-sink": "^0.5.0", | ||
"as-variant": "^0.4.0" | ||
}, | ||
@@ -35,0 +36,0 @@ "repository": { |
@@ -71,5 +71,5 @@ # AS-JSON | ||
**Serialize Object (Vec2):** ~7.29m ops/s | ||
**Serialize Object (Vec2):** ~7.20m ops/s | ||
**Deserialize Object (Vec2):** ~1.36m ops/s | ||
**Deserialize Object (Vec2):** ~2.2m ops/s | ||
@@ -76,0 +76,0 @@ **Serialize Array (int[4]):** ~1.4m ops/s |
@@ -9,5 +9,6 @@ import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js"; | ||
this.encodeStmts = []; | ||
this.decodeStmts = []; | ||
this.checkDecodeStmts = []; | ||
//public decodeStmts: string[] = []; | ||
this.setDataStmts = []; | ||
} | ||
//public checkDecodeStmts: string[] = []; | ||
visitMethodDeclaration() { } | ||
@@ -26,5 +27,15 @@ visitFieldDeclaration(node) { | ||
// @ts-ignore | ||
this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`); | ||
//this.decodeStmts.push( | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//); | ||
// @ts-ignore | ||
this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'); | ||
this.setDataStmts.push(`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) { | ||
this.${name} = JSON.parseObjectValue<${type}>(value); | ||
return; | ||
} | ||
`); | ||
// @ts-ignore | ||
//this.checkDecodeStmts.push( | ||
// ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n' | ||
//); | ||
} | ||
@@ -43,3 +54,2 @@ visitClassDeclaration(node) { | ||
serializeFunc = ` | ||
@inline | ||
__JSON_Serialize(): string { | ||
@@ -52,3 +62,2 @@ return \`{${this.encodeStmts.join("")}}\`; | ||
serializeFunc = ` | ||
@inline | ||
__JSON_Serialize(): string { | ||
@@ -59,15 +68,30 @@ return "{}"; | ||
} | ||
const deserializeFunc = ` | ||
@inline | ||
__JSON_Deserialize<T>(values: Map<string, string>): T { | ||
${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""} | ||
return { | ||
${ | ||
/*const deserializeFunc = ` | ||
__JSON_Deserialize<T>(values: Map<string, string>): T { | ||
${ | ||
process.argv.includes("--debugJSON") | ||
? this.checkDecodeStmts.join("else") | ||
: "" | ||
} | ||
return { | ||
${ | ||
// @ts-ignore | ||
this.decodeStmts.join("") | ||
} | ||
} | ||
} | ||
`;*/ | ||
const setKeyFunc = ` | ||
__JSON_Set_Key(key: string, value: string): void { | ||
${ | ||
// @ts-ignore | ||
this.decodeStmts.join("")} | ||
} | ||
} | ||
this.setDataStmts.join("")} | ||
throw new Error("Cannot find key: " + key); | ||
} | ||
`; | ||
//console.log(setKeyFunc, deserializeFunc, serializeFunc) | ||
this.encodeStmts = []; | ||
this.decodeStmts = []; | ||
//this.decodeStmts = []; | ||
this.setDataStmts = []; | ||
//this.checkDecodeStmts = []; | ||
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node); | ||
@@ -77,4 +101,9 @@ node.members.push(serializedProperty); | ||
node.members.push(serializeMethod); | ||
const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node); | ||
node.members.push(deserializeMethod); | ||
//const deserializeMethod = SimpleParser.parseClassMember( | ||
// deserializeFunc, | ||
// node | ||
//); | ||
//node.members.push(deserializeMethod); | ||
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node); | ||
node.members.push(setDataMethod); | ||
} | ||
@@ -81,0 +110,0 @@ get name() { |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -17,4 +17,5 @@ import { | ||
public encodeStmts: string[] = []; | ||
public decodeStmts: string[] = []; | ||
public checkDecodeStmts: string[] = []; | ||
//public decodeStmts: string[] = []; | ||
public setDataStmts: string[] = []; | ||
//public checkDecodeStmts: string[] = []; | ||
@@ -37,10 +38,19 @@ visitMethodDeclaration(): void {} | ||
// @ts-ignore | ||
this.decodeStmts.push( | ||
`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//this.decodeStmts.push( | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//); | ||
// @ts-ignore | ||
this.setDataStmts.push( | ||
`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) { | ||
this.${name} = JSON.parseObjectValue<${type}>(value); | ||
return; | ||
} | ||
` | ||
); | ||
// @ts-ignore | ||
this.checkDecodeStmts.push( | ||
' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n' | ||
); | ||
//this.checkDecodeStmts.push( | ||
// ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n' | ||
//); | ||
} | ||
@@ -67,3 +77,2 @@ visitClassDeclaration(node: ClassDeclaration): void { | ||
serializeFunc = ` | ||
@inline | ||
__JSON_Serialize(): string { | ||
@@ -75,3 +84,2 @@ return \`{${this.encodeStmts.join("")}}\`; | ||
serializeFunc = ` | ||
@inline | ||
__JSON_Serialize(): string { | ||
@@ -82,4 +90,3 @@ return "{}"; | ||
} | ||
const deserializeFunc = ` | ||
@inline | ||
/*const deserializeFunc = ` | ||
__JSON_Deserialize<T>(values: Map<string, string>): T { | ||
@@ -98,5 +105,17 @@ ${ | ||
} | ||
`; | ||
`;*/ | ||
const setKeyFunc = ` | ||
__JSON_Set_Key(key: string, value: string): void { | ||
${ | ||
// @ts-ignore | ||
this.setDataStmts.join("") | ||
} | ||
throw new Error("Cannot find key: " + key); | ||
} | ||
` | ||
//console.log(setKeyFunc, deserializeFunc, serializeFunc) | ||
this.encodeStmts = []; | ||
this.decodeStmts = []; | ||
//this.decodeStmts = []; | ||
this.setDataStmts = []; | ||
//this.checkDecodeStmts = []; | ||
const serializedProperty = SimpleParser.parseClassMember( | ||
@@ -111,7 +130,12 @@ serializedProp, | ||
const deserializeMethod = SimpleParser.parseClassMember( | ||
deserializeFunc, | ||
//const deserializeMethod = SimpleParser.parseClassMember( | ||
// deserializeFunc, | ||
// node | ||
//); | ||
//node.members.push(deserializeMethod); | ||
const setDataMethod = SimpleParser.parseClassMember( | ||
setKeyFunc, | ||
node | ||
); | ||
node.members.push(deserializeMethod); | ||
node.members.push(setDataMethod); | ||
} | ||
@@ -118,0 +142,0 @@ get name(): string { |
56945
43.5%25
8.7%1566
47.32%2
100%+ Added
+ Added