Comparing version
@@ -15,4 +15,7 @@ { | ||
"./transform" | ||
] | ||
} | ||
], | ||
"bindings": "esm", | ||
"exportStart": "_start" | ||
}, | ||
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json" | ||
} |
import { JSON } from ".."; | ||
import { backSlashCode, quoteCode } from "../src/chars"; | ||
import { atoi_fast, unsafeCharCodeAt } from "../src/util"; | ||
import { HASH } from "util/hash"; | ||
@@ -26,7 +27,7 @@ @json | ||
if (unsafeCharCodeAt(key, 0) == 120) { | ||
to.x = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.x = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} else if (unsafeCharCodeAt(key, 0) == 121) { | ||
to.y = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.y = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} else if (unsafeCharCodeAt(key, 0) == 122) { | ||
to.z = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.z = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} | ||
@@ -38,3 +39,3 @@ } | ||
inStr = false; | ||
key = data.slice(last, pos); | ||
key = data.substring(last, pos); | ||
last = pos += 2; | ||
@@ -45,7 +46,7 @@ } | ||
if (unsafeCharCodeAt(key, 0) == 120) { | ||
to.x = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.x = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} else if (unsafeCharCodeAt(key, 0) == 121) { | ||
to.y = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.y = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} else if (unsafeCharCodeAt(key, 0) == 122) { | ||
to.z = atoi_fast<i32>(data.slice(last, pos - 1)) | ||
to.z = atoi_fast<i32>(data.substring(last, pos - 1)) | ||
} | ||
@@ -71,2 +72,5 @@ } | ||
bench("HASH String", () => { | ||
blackbox<number>(HASH("Hello")); | ||
}) | ||
// TODO: Make this allocate without crashing | ||
@@ -73,0 +77,0 @@ bench("Parse Object (Vec3)", () => { |
@@ -8,62 +8,18 @@ import { backSlashCode, quoteCode } from "./src/chars"; | ||
class Vec3 { | ||
x!: f32; | ||
y!: f32; | ||
z!: f32; | ||
@inline | ||
__JSON_Serialize(): string { | ||
return `{"x":${this.x.toString()},"y":${this.y.toString()},"z":${this.z.toString()}}`; | ||
} | ||
@inline | ||
__JSON_Deserialize(data: string, to: Vec3): Vec3 { | ||
let last = 1; | ||
let char = 0; | ||
let inStr = false; | ||
let key: string | null = null; | ||
let pos = 0; | ||
for (; pos < data.length - 1; pos++) { | ||
char = unsafeCharCodeAt(data, pos); | ||
if (inStr === false && char === quoteCode) { | ||
if (key != null) { | ||
if (key == "x") { | ||
to.x = f32.parse(data.slice(last, pos - 1)) | ||
} else if (key == "y") { | ||
to.y = f32.parse(data.slice(last, pos - 1)) | ||
} else if (key == "z") { | ||
to.z = f32.parse(data.slice(last, pos - 1)) | ||
} | ||
} | ||
last = ++pos; | ||
inStr = true; | ||
} else if (char === quoteCode && unsafeCharCodeAt(data, pos - 1) != backSlashCode) { | ||
inStr = false; | ||
key = data.slice(last, pos); | ||
last = pos += 2; | ||
} | ||
} | ||
if (key != null) { | ||
if (key == "x") { | ||
to.x = f32.parse(data.slice(last, pos - 1)) | ||
} else if (key == "y") { | ||
to.y = f32.parse(data.slice(last, pos - 1)) | ||
} else if (key == "z") { | ||
to.z = f32.parse(data.slice(last, pos - 1)) | ||
} | ||
} | ||
return to; | ||
} | ||
x: i32; | ||
y: i32; | ||
z: i32; | ||
} | ||
const vec: Vec3 = { | ||
x: 3.4, | ||
y: 1.2, | ||
z: 8.3 | ||
x: 3, | ||
y: 1, | ||
z: 8 | ||
} | ||
const serializedVec3 = vec.__JSON_Serialize(); | ||
const serializedVec3 = JSON.stringify(vec); | ||
console.log(serializedVec3); | ||
const parsedVec3 = vec.__JSON_Deserialize(serializedVec3, new Vec3()); | ||
console.log(parsedVec3.__JSON_Serialize()); | ||
const parsedVec3 = JSON.parse<Vec3>(serializedVec3); | ||
console.log(JSON.stringify(parsedVec3)); | ||
@@ -70,0 +26,0 @@ console.log(`atoi_fast("429496729"): ${atoi_fast<i32>("429496729")}`); |
{ | ||
"name": "json-as", | ||
"version": "0.5.32", | ||
"version": "0.5.33", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -5,0 +5,0 @@ "types": "assembly/index.ts", |
@@ -1,2 +0,2 @@ | ||
import { getName, toString, isStdlib } from "visitor-as/dist/utils.js"; | ||
import { toString, isStdlib } from "visitor-as/dist/utils.js"; | ||
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js"; | ||
@@ -22,37 +22,2 @@ import { Transform } from "assemblyscript/dist/transform.js"; | ||
visitMethodDeclaration() { } | ||
visitFieldDeclaration(node) { | ||
if (toString(node).startsWith("static")) | ||
return; | ||
const lineText = toString(node); | ||
if (lineText.startsWith("private")) | ||
return; | ||
const name = getName(node); | ||
if (!node.type) { | ||
throw new Error(`Field ${name} is missing a type declaration`); | ||
} | ||
let type = getName(node.type); | ||
// @ts-ignore | ||
if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) { | ||
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`); | ||
} | ||
else { | ||
this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`); | ||
} | ||
this.currentClass.keys.push(name); | ||
this.currentClass.types.push(type); | ||
// @ts-ignore | ||
//this.decodeStmts.push( | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//); | ||
// @ts-ignore | ||
this.currentClass.setDataStmts.push(`if (key == "${name}") { | ||
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' | ||
//); | ||
} | ||
visitClassDeclaration(node) { | ||
@@ -71,3 +36,3 @@ var _c; | ||
return; | ||
// Prevent from being triggered twice | ||
// Prevent from being triggered twice. | ||
for (const member of node.members) { | ||
@@ -99,3 +64,31 @@ if (member.name.text == "__JSON_Serialize") | ||
const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])]; | ||
this.visit(members); | ||
for (const mem of members) { | ||
if (mem.type && mem.type.name && mem.type.name.identifier.text) { | ||
const member = mem; | ||
if (toString(member).startsWith("static")) | ||
return; | ||
const lineText = toString(member); | ||
if (lineText.startsWith("private")) | ||
return; | ||
// @ts-ignore | ||
const type = member.type.name.identifier.text; | ||
const name = member.name.text; | ||
this.currentClass.keys.push(name); | ||
// @ts-ignore | ||
this.currentClass.types.push(type); | ||
// @ts-ignore | ||
if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) { | ||
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`); | ||
} | ||
else { | ||
this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`); | ||
} | ||
// @ts-ignore | ||
this.currentClass.setDataStmts.push(`if (key == "${name}") { | ||
this.${name} = JSON.parseObjectValue<${type}>(value); | ||
return; | ||
} | ||
`); | ||
} | ||
} | ||
let serializeFunc = ""; | ||
@@ -102,0 +95,0 @@ if (this.currentClass.encodeStmts.length > 0) { |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.32", | ||
"version": "0.5.33", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -13,3 +13,5 @@ "main": "./lib/index.js", | ||
"license": "MIT", | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"assemblyscript": "^0.27.1" | ||
}, | ||
"dependencies": {}, | ||
@@ -16,0 +18,0 @@ "repository": { |
@@ -7,3 +7,3 @@ import { | ||
} from "assemblyscript/dist/assemblyscript"; | ||
import { getName, toString, isStdlib } from "visitor-as/dist/utils.js"; | ||
import { toString, isStdlib } from "visitor-as/dist/utils.js"; | ||
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js"; | ||
@@ -28,44 +28,2 @@ import { Transform } from "assemblyscript/dist/transform.js"; | ||
visitMethodDeclaration(): void { } | ||
visitFieldDeclaration(node: FieldDeclaration): void { | ||
if (toString(node).startsWith("static")) return; | ||
const lineText = toString(node); | ||
if (lineText.startsWith("private")) return; | ||
const name = getName(node); | ||
if (!node.type) { | ||
throw new Error(`Field ${name} is missing a type declaration`); | ||
} | ||
let type = getName(node.type); | ||
// @ts-ignore | ||
if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) { | ||
this.currentClass.encodeStmts.push( | ||
`"${name}":\${this.${name}.toString()},` | ||
); | ||
} else { | ||
this.currentClass.encodeStmts.push( | ||
`"${name}":\${JSON.stringify<${type}>(this.${name})},` | ||
); | ||
} | ||
this.currentClass.keys.push(name); | ||
this.currentClass.types.push(type); | ||
// @ts-ignore | ||
//this.decodeStmts.push( | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//); | ||
// @ts-ignore | ||
this.currentClass.setDataStmts.push( | ||
`if (key == "${name}") { | ||
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' | ||
//); | ||
} | ||
visitClassDeclaration(node: ClassDeclaration): void { | ||
@@ -81,3 +39,3 @@ const className = node.name.text; | ||
// Prevent from being triggered twice | ||
// Prevent from being triggered twice. | ||
for (const member of node.members) { | ||
@@ -110,4 +68,37 @@ if (member.name.text == "__JSON_Serialize") return; | ||
const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])] | ||
this.visit(members); | ||
for (const mem of members) { | ||
if (mem.type && mem.type.name && mem.type.name.identifier.text) { | ||
const member: FieldDeclaration = mem; | ||
if (toString(member).startsWith("static")) return; | ||
const lineText = toString(member); | ||
if (lineText.startsWith("private")) return; | ||
// @ts-ignore | ||
const type = member.type.name.identifier.text; | ||
const name = member.name.text; | ||
this.currentClass.keys.push(name); | ||
// @ts-ignore | ||
this.currentClass.types.push(type); | ||
// @ts-ignore | ||
if (["u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "f64"].includes(type.toLowerCase())) { | ||
this.currentClass.encodeStmts.push( | ||
`"${name}":\${this.${name}.toString()},` | ||
); | ||
} else { | ||
this.currentClass.encodeStmts.push( | ||
`"${name}":\${JSON.stringify<${type}>(this.${name})},` | ||
); | ||
} | ||
// @ts-ignore | ||
this.currentClass.setDataStmts.push( | ||
`if (key == "${name}") { | ||
this.${name} = JSON.parseObjectValue<${type}>(value); | ||
return; | ||
} | ||
` | ||
); | ||
} | ||
} | ||
let serializeFunc = ""; | ||
@@ -145,3 +136,3 @@ | ||
` | ||
const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node); | ||
@@ -168,3 +159,3 @@ node.members.push(serializeMethod); | ||
const transformer = new AsJSONTransform(); | ||
// Sort the sources so that user scripts are visited last | ||
@@ -182,3 +173,3 @@ const sources = parser.sources.filter(source => !isStdlib(source)).sort((_a, _b) => { | ||
}) | ||
// Loop over every source | ||
@@ -185,0 +176,0 @@ for (const source of sources) { |
73603
-1.82%1647
-2.89%