diff --git a/packages/js/plugins/ws/README.md b/packages/js/plugins/ws/README.md index ced32bc5d3..ea279f8d88 100644 --- a/packages/js/plugins/ws/README.md +++ b/packages/js/plugins/ws/README.md @@ -3,57 +3,9 @@ WebSocket plugin allows Polywrap Client to interact with WebSocket servers. ## interface - -``` typescript -# subset of JS MessageEvent interface -type Message { - data: String! - origin: String! - lastEventId: String! -} - -# path to WRAP method -type Callback { - uri: String!, - method: String! -} - -# optional fields are `Number | null` instead of `Option` -type Number { - value: Int! -} - -type Module { - # create a socket with id - ## can return after `timeout` if the server is not responding - open(url: String!, timeout: Number): Int! - - # close socket `id` - close(id: Int!): Boolean - - # send message via socket `id` - send(id: Int!, message: String!): Boolean - - # pass all messages to callback - addCallback(id: Int!, callback: Callback!): Boolean - - # stop passing messages to callback - removeCallback(id: Int!, callback: Callback!): Boolean - - # save messages to ws plugin cache - addCache(id: Int!): Boolean - - # stop caching messages - removeCache(id: Int!): Boolean - - # get [messages], flush cache - ## can wait until receives `min` events or reaches `timeout` - receive(id: Int!, min: Number, timeout: Number): [Message!]! -} -``` +See [schema.graphql](./src/schema.graphql). ## callback - Every incoming WebSocket message can be passed to a callback function in another wrapper. Use `addCallback` to start passing messages and `removeCallback` to stop. The callback function is expected to have a parameter `data`, i.e. `foo(data: string)`. ``` typescript diff --git a/packages/js/plugins/ws/src/__tests__/e2e/e2e.spec.ts b/packages/js/plugins/ws/src/__tests__/e2e/e2e.spec.ts index b26c36605d..df8e969fd9 100644 --- a/packages/js/plugins/ws/src/__tests__/e2e/e2e.spec.ts +++ b/packages/js/plugins/ws/src/__tests__/e2e/e2e.spec.ts @@ -69,7 +69,7 @@ describe("WebSocket plugin", () => { method: "open", args: { url: "ws://localhost:1235", - timeout: { value: 50 } + timeout: 50 } }) }); @@ -347,7 +347,7 @@ describe("WebSocket plugin", () => { method: "receive", args: { id, - timeout: { value: 250 } + timeout: 250 } }) @@ -378,7 +378,7 @@ describe("WebSocket plugin", () => { method: "receive", args: { id, - min: { value: 2 } + min: 2 } }) @@ -408,8 +408,8 @@ describe("WebSocket plugin", () => { method: "receive", args: { id, - timeout: { value: 110 }, - min: { value: 2 } + timeout: 110, + min: 2 } }) @@ -439,8 +439,8 @@ describe("WebSocket plugin", () => { method: "receive", args: { id, - timeout: { value: 300 }, - min: { value: 1 } + timeout: 300, + min: 1 } }) diff --git a/packages/js/plugins/ws/src/__tests__/e2e/integration.spec.ts b/packages/js/plugins/ws/src/__tests__/e2e/integration.spec.ts index db0292f602..4578dddeda 100644 --- a/packages/js/plugins/ws/src/__tests__/e2e/integration.spec.ts +++ b/packages/js/plugins/ws/src/__tests__/e2e/integration.spec.ts @@ -106,7 +106,7 @@ describe("e2e tests for WsPlugin", () => { class MemoryPlugin extends PluginModule<{}> { set(args: { key: string, value: string }, _client: Client): boolean { value[args.key] = args.value - return true + return true } get(args: { key: string }, _client: Client): string | null { return value[args.key] ?? null @@ -140,7 +140,7 @@ describe("e2e tests for WsPlugin", () => { uri, method: "callback" }, - message: "test" + message: "test" } }); diff --git a/packages/js/plugins/ws/src/__tests__/e2e/integration/schema.graphql b/packages/js/plugins/ws/src/__tests__/e2e/integration/schema.graphql index 5ac685e4e5..1b1a3d2e74 100644 --- a/packages/js/plugins/ws/src/__tests__/e2e/integration/schema.graphql +++ b/packages/js/plugins/ws/src/__tests__/e2e/integration/schema.graphql @@ -24,6 +24,6 @@ type Module { get( url: String!, - timeout: Int! + timeout: UInt32! ): [String!]! } diff --git a/packages/js/plugins/ws/src/__tests__/e2e/integration/src/index.ts b/packages/js/plugins/ws/src/__tests__/e2e/integration/src/index.ts index e213428d5e..7345e8c726 100644 --- a/packages/js/plugins/ws/src/__tests__/e2e/integration/src/index.ts +++ b/packages/js/plugins/ws/src/__tests__/e2e/integration/src/index.ts @@ -7,6 +7,7 @@ import { Args_subscribeAndSend, Args_get } from "./wrap"; +import { Box } from "@polywrap/wasm-as"; export function send(args: Args_send): boolean { const id = WS_Module.open({ @@ -68,9 +69,9 @@ export function get(args: Args_get): string[] { WS_Module.addCache({ id - }).unwrap().unwrap() + }).unwrap() - const messages = WS_Module.receive({ id, timeout: { value: args.timeout } }).unwrap(); + const messages = WS_Module.receive({ id, timeout: Box.from(args.timeout) }).unwrap(); const data: string[] = messages.map((msg) => msg.data); diff --git a/packages/js/plugins/ws/src/index.ts b/packages/js/plugins/ws/src/index.ts index e68e56851e..8f1a930018 100644 --- a/packages/js/plugins/ws/src/index.ts +++ b/packages/js/plugins/ws/src/index.ts @@ -32,7 +32,7 @@ export class WsPlugin extends Module { if (args.timeout) { setTimeout(() => { reject(new Error("timeout reached")); - }, args.timeout.value); + }, args.timeout); } this._sockets[id].onopen = () => { resolve(id); @@ -40,10 +40,7 @@ export class WsPlugin extends Module { }); } - public async close( - args: Args_close, - _client: Client - ): Promise { + public async close(args: Args_close, _client: Client): Promise { this._sockets[args.id].close(); return await new Promise((resolve) => { this._sockets[args.id].onclose = () => { @@ -52,12 +49,12 @@ export class WsPlugin extends Module { }); } - public send(args: Args_send, _client: Client): boolean | null { + public send(args: Args_send, _client: Client): boolean { this._sockets[args.id].send(args.message); return true; } - public addCallback(args: Args_addCallback, _client: Client): boolean | null { + public addCallback(args: Args_addCallback, _client: Client): boolean { const callbackId = this._callbackId(args.callback); this._callbacks[callbackId] = async (msg) => { await _client.invoke<{ callback: boolean }>({ @@ -73,10 +70,7 @@ export class WsPlugin extends Module { return true; } - public removeCallback( - args: Args_removeCallback, - _client: Client - ): boolean | null { + public removeCallback(args: Args_removeCallback, _client: Client): boolean { const callbackId = this._callbackId(args.callback); this._sockets[args.id].removeEventListener( "message", @@ -85,7 +79,7 @@ export class WsPlugin extends Module { return true; } - public addCache(args: Args_addCache, _client: Client): boolean | null { + public addCache(args: Args_addCache, _client: Client): boolean { const callback = { uri: args.id.toString(), method: "cache" }; const callbackId = this._callbackId(callback); this._caches[args.id] = []; @@ -109,7 +103,7 @@ export class WsPlugin extends Module { return true; } - public removeCache(args: Args_removeCache, _client: Client): boolean | null { + public removeCache(args: Args_removeCache, _client: Client): boolean { const callback = { uri: args.id.toString(), method: "cache" }; const callbackId = this._callbackId(callback); this._sockets[args.id].removeEventListener( @@ -135,7 +129,7 @@ export class WsPlugin extends Module { if (args.min) { interval = setInterval(() => { if (args.min) { - if (this._caches[args.id].length >= args.min.value) { + if (this._caches[args.id].length >= args.min) { clearInterval(interval); clear(); } @@ -146,7 +140,7 @@ export class WsPlugin extends Module { setTimeout(() => { clearInterval(interval); clear(); - }, args.timeout.value); + }, args.timeout); } if (!args.timeout && !args.min) { clear(); diff --git a/packages/js/plugins/ws/src/schema.graphql b/packages/js/plugins/ws/src/schema.graphql index 1f5f375777..d35469f91c 100644 --- a/packages/js/plugins/ws/src/schema.graphql +++ b/packages/js/plugins/ws/src/schema.graphql @@ -1,25 +1,57 @@ +"""Subset of JS MessageEvent interface""" type Message { - data: String! - origin: String! - lastEventId: String! + data: String! + origin: String! + lastEventId: String! } type Callback { - uri: String!, - method: String! -} - -type Number { - value: Int! + """WRAP Module URI""" + uri: String! + """WRAP Module Method""" + method: String! } type Module { - open(url: String!, timeout: Number): Int! - close(id: Int!): Boolean - send(id: Int!, message: String!): Boolean - addCallback(id: Int!, callback: Callback!): Boolean - removeCallback(id: Int!, callback: Callback!): Boolean - addCache(id: Int!): Boolean - removeCache(id: Int!): Boolean - receive(id: Int!, min: Number, timeout: Number): [Message!]! + """ + create a socket with id, can return after `timeout` + if the server is not responding. Returns the socket `id` + """ + open(url: String!, timeout: UInt32): UInt32! + + """ + close socket `id` + """ + close(id: UInt32!): Boolean! + + """ + send message via socket `id` + """ + send(id: UInt32!, message: String!): Boolean! + + """ + send all messages to callback for socket `id` + """ + addCallback(id: UInt32!, callback: Callback!): Boolean! + + """ + stop sending messages to callback for socket `id` + """ + removeCallback(id: UInt32!, callback: Callback!): Boolean! + + """ + save messages to ws plugin cache for socket `id` + """ + addCache(id: UInt32!): Boolean! + + """ + stop caching messages for socket `id` + """ + removeCache(id: UInt32!): Boolean! + + """ + get messages and flush cache, + can wait until receives `min` events or reaches `timeout` + """ + receive(id: UInt32!, min: UInt32, timeout: UInt32): [Message!]! } diff --git a/packages/schema/bind/src/bindings/assemblyscript/functions.ts b/packages/schema/bind/src/bindings/assemblyscript/functions.ts index 44c34685a7..f58c5d5e12 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/functions.ts +++ b/packages/schema/bind/src/bindings/assemblyscript/functions.ts @@ -51,14 +51,10 @@ export const toWasmInit: MustacheFn = () => { type = type.substring(0, type.length - 1); } else { const nullType = toWasm()(value, render); - const optional = "Option"; const nullOptional = "| null"; if (nullType.endsWith(nullOptional)) { return "null"; - } else if (nullType.startsWith(optional)) { - type = nullType.substring(6); - return `Option.None${type}()`; } } @@ -250,7 +246,7 @@ const applyOptional = ( ) { return `${type} | null`; } else { - return `Option<${type}>`; + return `Box<${type}> | null`; } } else { return type; diff --git a/packages/schema/bind/src/bindings/assemblyscript/types.ts b/packages/schema/bind/src/bindings/assemblyscript/types.ts index 545f4611c1..6961a87298 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/types.ts +++ b/packages/schema/bind/src/bindings/assemblyscript/types.ts @@ -199,6 +199,9 @@ const keywords = { Float64Array: "Float64Array", TemplateStringsArray: "TemplateStringsArray", Error: "Error", + Result: "Result", + Box: "Box", + JSON: "JSON", // runtime abort: "abort", trace: "trace", diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/deserialize_enum.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/deserialize_enum.mustache index e45e533f3c..6702368f21 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/deserialize_enum.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/deserialize_enum.mustache @@ -8,19 +8,19 @@ if (reader.isNextString()) { } {{/required}} {{^required}} -let value: Option; +let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.get{{type}}Value(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitize{{type}}Value(value.unwrap()); } } else { - value = Option.None(); + value = null; } {{/required}} diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/env-type/index-ts.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/env-type/index-ts.mustache index 1bd693e9c0..8199149c0d 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/env-type/index-ts.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/env-type/index-ts.mustache @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/module-type/index-ts.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/module-type/index-ts.mustache index 46c901b31f..ed45b7686e 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/module-type/index-ts.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/module-type/index-ts.mustache @@ -1,7 +1,7 @@ import { wrap_subinvoke, wrap_subinvokeImplementation, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/object-type/index-ts.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/object-type/index-ts.mustache index bd6dec16ce..3d914e5ae1 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/object-type/index-ts.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/imported/object-type/index-ts.mustache @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/object-type/index-ts.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/object-type/index-ts.mustache index 1bd693e9c0..8199149c0d 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/object-type/index-ts.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/object-type/index-ts.mustache @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/serialization_imports.mustache b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/serialization_imports.mustache index 9ded19f5d4..933d73f458 100644 --- a/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/serialization_imports.mustache +++ b/packages/schema/bind/src/bindings/assemblyscript/wasm/templates/serialization_imports.mustache @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/templates/package.json b/packages/templates/package.json index 98c2ddb572..6bc9138bc4 100644 --- a/packages/templates/package.json +++ b/packages/templates/package.json @@ -8,7 +8,9 @@ "url": "https://github.com/polywrap/monorepo.git" }, "scripts": { - "test": "jest --passWithNoTests --runInBand --verbose", + "wasm-linked:up": "cp ./polywrap.wasm-linked* ./wasm/assemblyscript/", + "wasm-linked:down": "rm ./wasm/assemblyscript/polywrap.wasm-linked*", + "test": "yarn wasm-linked:up; jest --passWithNoTests --runInBand --verbose; yarn wasm-linked:down", "test:ci": "yarn test" }, "devDependencies": { diff --git a/packages/templates/polywrap.wasm-linked.build.yaml b/packages/templates/polywrap.wasm-linked.build.yaml new file mode 100644 index 0000000000..959488ab0c --- /dev/null +++ b/packages/templates/polywrap.wasm-linked.build.yaml @@ -0,0 +1,11 @@ +format: 0.1.0 +docker: + name: template-wasm-as +config: + node_version: "16.13.0" + include: + - ./package.json + - ./src +linked_packages: + - name: "@polywrap/wasm-as" + path: ../../../wasm/as diff --git a/packages/templates/polywrap.wasm-linked.yaml b/packages/templates/polywrap.wasm-linked.yaml new file mode 100644 index 0000000000..294a567ae7 --- /dev/null +++ b/packages/templates/polywrap.wasm-linked.yaml @@ -0,0 +1,9 @@ +format: 0.2.0 +project: + name: template-wasm-as + type: wasm/assemblyscript +source: + module: ./src/index.ts + schema: ./src/schema.graphql +extensions: + build: ./polywrap.wasm-linked.build.yaml diff --git a/packages/templates/tests.spec.ts b/packages/templates/tests.spec.ts index 0285699310..248c72102c 100644 --- a/packages/templates/tests.spec.ts +++ b/packages/templates/tests.spec.ts @@ -21,7 +21,7 @@ describe("Templates", () => { build: "CI=false yarn build", }, assemblyscript: { - build: "yarn build", + build: "npx polywrap build -m ./polywrap.wasm-linked.yaml", test: "yarn test:e2e", }, rust: { diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/index.ts index f0078d83b9..7d2d0c8391 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/serialization.ts index 4bf9c7a830..9ed99bc7a3 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/AnotherType/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/index.ts index 16e451dd4e..4162a95785 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/serialization.ts index 847dde1269..d3f1ee9fb8 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomMapValue/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/index.ts index 917c30a1be..64b2cdf31c 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON @@ -18,7 +18,7 @@ export class CustomType { str: string; optStr: string | null; u: u32; - optU: Option; + optU: Box | null; _u8: u8; _u16: u16; _u32: u32; @@ -35,13 +35,13 @@ export class CustomType { bytes: ArrayBuffer; optBytes: ArrayBuffer | null; _boolean: bool; - optBoolean: Option; + optBoolean: Box | null; uArray: Array; uOptArray: Array | null; - optUOptArray: Array> | null; + optUOptArray: Array | null> | null; optStrOptArray: Array | null; uArrayArray: Array>; - uOptArrayOptArray: Array> | null>; + uOptArrayOptArray: Array | null> | null>; uArrayOptArrayArray: Array> | null>; crazyArray: Array | null>> | null> | null; object: Types.AnotherType; @@ -49,9 +49,9 @@ export class CustomType { objectArray: Array; optObjectArray: Array | null; en: Types.CustomEnum; - optEnum: Option; + optEnum: Box | null; enumArray: Array; - optEnumArray: Array> | null; + optEnumArray: Array | null> | null; map: Map; mapOfArr: Map>; mapOfObj: Map; diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/serialization.ts index e033193852..b555b83786 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/CustomType/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, @@ -38,7 +38,7 @@ export function writeCustomType(writer: Write, type: CustomType): void { writer.writeString("u"); writer.writeUInt32(type.u); writer.context().pop(); - writer.context().push("optU", "Option", "writing property"); + writer.context().push("optU", "Box | null", "writing property"); writer.writeString("optU"); writer.writeOptionalUInt32(type.optU); writer.context().pop(); @@ -106,7 +106,7 @@ export function writeCustomType(writer: Write, type: CustomType): void { writer.writeString("boolean"); writer.writeBool(type._boolean); writer.context().pop(); - writer.context().push("optBoolean", "Option", "writing property"); + writer.context().push("optBoolean", "Box | null", "writing property"); writer.writeString("optBoolean"); writer.writeOptionalBool(type.optBoolean); writer.context().pop(); @@ -122,9 +122,9 @@ export function writeCustomType(writer: Write, type: CustomType): void { writer.writeUInt32(item); }); writer.context().pop(); - writer.context().push("optUOptArray", "Array> | null", "writing property"); + writer.context().push("optUOptArray", "Array | null> | null", "writing property"); writer.writeString("optUOptArray"); - writer.writeOptionalArray(type.optUOptArray, (writer: Write, item: Option): void => { + writer.writeOptionalArray(type.optUOptArray, (writer: Write, item: Box | null): void => { writer.writeOptionalUInt32(item); }); writer.context().pop(); @@ -142,10 +142,10 @@ export function writeCustomType(writer: Write, type: CustomType): void { }); }); writer.context().pop(); - writer.context().push("uOptArrayOptArray", "Array> | null>", "writing property"); + writer.context().push("uOptArrayOptArray", "Array | null> | null>", "writing property"); writer.writeString("uOptArrayOptArray"); - writer.writeArray(type.uOptArrayOptArray, (writer: Write, item: Array> | null): void => { - writer.writeOptionalArray(item, (writer: Write, item: Option): void => { + writer.writeArray(type.uOptArrayOptArray, (writer: Write, item: Array | null> | null): void => { + writer.writeOptionalArray(item, (writer: Write, item: Box | null): void => { writer.writeOptionalUInt32(item); }); }); @@ -204,7 +204,7 @@ export function writeCustomType(writer: Write, type: CustomType): void { writer.writeString("en"); writer.writeInt32(type.en); writer.context().pop(); - writer.context().push("optEnum", "Option", "writing property"); + writer.context().push("optEnum", "Box | null", "writing property"); writer.writeString("optEnum"); writer.writeOptionalInt32(type.optEnum); writer.context().pop(); @@ -214,9 +214,9 @@ export function writeCustomType(writer: Write, type: CustomType): void { writer.writeInt32(item); }); writer.context().pop(); - writer.context().push("optEnumArray", "Array> | null", "writing property"); + writer.context().push("optEnumArray", "Array | null> | null", "writing property"); writer.writeString("optEnumArray"); - writer.writeOptionalArray(type.optEnumArray, (writer: Write, item: Option): void => { + writer.writeOptionalArray(type.optEnumArray, (writer: Write, item: Box | null): void => { writer.writeOptionalInt32(item); }); writer.context().pop(); @@ -284,7 +284,7 @@ export function readCustomType(reader: Read): CustomType { let _optStr: string | null = null; let _u: u32 = 0; let _uSet: bool = false; - let _optU: Option = Option.None(); + let _optU: Box | null = null; let _u8: u8 = 0; let _u8Set: bool = false; let _u16: u16 = 0; @@ -313,15 +313,15 @@ export function readCustomType(reader: Read): CustomType { let _optBytes: ArrayBuffer | null = null; let _boolean: bool = false; let _booleanSet: bool = false; - let _optBoolean: Option = Option.None(); + let _optBoolean: Box | null = null; let _uArray: Array = []; let _uArraySet: bool = false; let _uOptArray: Array | null = null; - let _optUOptArray: Array> | null = null; + let _optUOptArray: Array | null> | null = null; let _optStrOptArray: Array | null = null; let _uArrayArray: Array> = []; let _uArrayArraySet: bool = false; - let _uOptArrayOptArray: Array> | null> = []; + let _uOptArrayOptArray: Array | null> | null> = []; let _uOptArrayOptArraySet: bool = false; let _uArrayOptArrayArray: Array> | null> = []; let _uArrayOptArrayArraySet: bool = false; @@ -334,10 +334,10 @@ export function readCustomType(reader: Read): CustomType { let _optObjectArray: Array | null = null; let _en: Types.CustomEnum = 0; let _enSet: bool = false; - let _optEnum: Option = Option.None(); + let _optEnum: Box | null = null; let _enumArray: Array = []; let _enumArraySet: bool = false; - let _optEnumArray: Array> | null = null; + let _optEnumArray: Array | null> | null = null; let _map: Map = new Map(); let _mapSet: bool = false; let _mapOfArr: Map> = new Map>(); @@ -372,7 +372,7 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "optU") { - reader.context().push(field, "Option", "type found, reading property"); + reader.context().push(field, "Box | null", "type found, reading property"); _optU = reader.readOptionalUInt32(); reader.context().pop(); } @@ -469,7 +469,7 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "optBoolean") { - reader.context().push(field, "Option", "type found, reading property"); + reader.context().push(field, "Box | null", "type found, reading property"); _optBoolean = reader.readOptionalBool(); reader.context().pop(); } @@ -489,8 +489,8 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "optUOptArray") { - reader.context().push(field, "Array> | null", "type found, reading property"); - _optUOptArray = reader.readOptionalArray((reader: Read): Option => { + reader.context().push(field, "Array | null> | null", "type found, reading property"); + _optUOptArray = reader.readOptionalArray((reader: Read): Box | null => { return reader.readOptionalUInt32(); }); reader.context().pop(); @@ -513,9 +513,9 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "uOptArrayOptArray") { - reader.context().push(field, "Array> | null>", "type found, reading property"); - _uOptArrayOptArray = reader.readArray((reader: Read): Array> | null => { - return reader.readOptionalArray((reader: Read): Option => { + reader.context().push(field, "Array | null> | null>", "type found, reading property"); + _uOptArrayOptArray = reader.readArray((reader: Read): Array | null> | null => { + return reader.readOptionalArray((reader: Read): Box | null => { return reader.readOptionalUInt32(); }); }); @@ -597,21 +597,21 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "optEnum") { - reader.context().push(field, "Option", "type found, reading property"); - let value: Option; + reader.context().push(field, "Box | null", "type found, reading property"); + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getCustomEnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeCustomEnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } _optEnum = value; reader.context().pop(); @@ -632,22 +632,22 @@ export function readCustomType(reader: Read): CustomType { reader.context().pop(); } else if (field == "optEnumArray") { - reader.context().push(field, "Array> | null", "type found, reading property"); - _optEnumArray = reader.readOptionalArray((reader: Read): Option => { - let value: Option; + reader.context().push(field, "Array | null> | null", "type found, reading property"); + _optEnumArray = reader.readOptionalArray((reader: Read): Box | null => { + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getCustomEnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeCustomEnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } return value; }); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/index.ts index e23475d9bf..6e9417c587 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON @@ -17,7 +17,7 @@ import * as Types from ".."; export class Env { prop: string; optProp: string | null; - optMap: Map> | null; + optMap: Map | null> | null; static toBuffer(type: Env): ArrayBuffer { return serializeEnv(type); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/serialization.ts index b6fbadfd37..84186a0ea1 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/Env/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, @@ -34,11 +34,11 @@ export function writeEnv(writer: Write, type: Env): void { writer.writeString("optProp"); writer.writeOptionalString(type.optProp); writer.context().pop(); - writer.context().push("optMap", "Map> | null", "writing property"); + writer.context().push("optMap", "Map | null> | null", "writing property"); writer.writeString("optMap"); writer.writeOptionalExtGenericMap(type.optMap, (writer: Write, key: string) => { writer.writeString(key); - }, (writer: Write, value: Option): void => { + }, (writer: Write, value: Box | null): void => { writer.writeOptionalInt32(value); }); writer.context().pop(); @@ -56,7 +56,7 @@ export function readEnv(reader: Read): Env { let _prop: string = ""; let _propSet: bool = false; let _optProp: string | null = null; - let _optMap: Map> | null = null; + let _optMap: Map | null> | null = null; while (numFields > 0) { numFields--; @@ -75,10 +75,10 @@ export function readEnv(reader: Read): Env { reader.context().pop(); } else if (field == "optMap") { - reader.context().push(field, "Map> | null", "type found, reading property"); + reader.context().push(field, "Map | null> | null", "type found, reading property"); _optMap = reader.readOptionalExtGenericMap((reader: Read): string => { return reader.readString(); - }, (reader: Read): Option => { + }, (reader: Read): Box | null => { return reader.readOptionalInt32(); }); reader.context().pop(); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/Module/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/Module/serialization.ts index 4fe8792357..d33172ef42 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/Module/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/Module/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, @@ -16,9 +16,9 @@ export class Args_moduleMethod { str: string; optStr: string | null; en: Types.CustomEnum; - optEnum: Option; + optEnum: Box | null; enumArray: Array; - optEnumArray: Array> | null; + optEnumArray: Array | null> | null; map: Map; mapOfArr: Map>; mapOfMap: Map>; @@ -36,10 +36,10 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe let _optStr: string | null = null; let _en: Types.CustomEnum = 0; let _enSet: bool = false; - let _optEnum: Option = Option.None(); + let _optEnum: Box | null = null; let _enumArray: Array = []; let _enumArraySet: bool = false; - let _optEnumArray: Array> | null = null; + let _optEnumArray: Array | null> | null = null; let _map: Map = new Map(); let _mapSet: bool = false; let _mapOfArr: Map> = new Map>(); @@ -81,21 +81,21 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe reader.context().pop(); } else if (field == "optEnum") { - reader.context().push(field, "Option", "type found, reading property"); - let value: Option; + reader.context().push(field, "Box | null", "type found, reading property"); + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getCustomEnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeCustomEnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } _optEnum = value; reader.context().pop(); @@ -116,22 +116,22 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe reader.context().pop(); } else if (field == "optEnumArray") { - reader.context().push(field, "Array> | null", "type found, reading property"); - _optEnumArray = reader.readOptionalArray((reader: Read): Option => { - let value: Option; + reader.context().push(field, "Array | null> | null", "type found, reading property"); + _optEnumArray = reader.readOptionalArray((reader: Read): Box | null => { + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getCustomEnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeCustomEnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } return value; }); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/else/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/else/index.ts index e3b07794d9..48f590f30b 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/else/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/else/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/else/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/else/serialization.ts index f89184e286..08a0df0df3 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/else/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/else/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/index.ts index 8957633043..54d80f554c 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/serialization.ts index 98b67d08ff..1835851b49 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_AnotherObject/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Env/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Env/serialization.ts index aff902a1bb..48866a4a00 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Env/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Env/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/index.ts index 1f5887a141..86b76df375 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/index.ts @@ -1,7 +1,7 @@ import { wrap_subinvoke, wrap_subinvokeImplementation, - Option, + Box, BigInt, BigNumber, JSON, diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/serialization.ts index c6b5c83eaa..b2efd2df5e 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Module/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, @@ -16,16 +16,16 @@ export class Args_importedMethod { str: string; optStr: string | null; u: u32; - optU: Option; - uArrayArray: Array> | null>; + optU: Box | null; + uArrayArray: Array | null> | null>; object: Types.TestImport_Object; optObject: Types.TestImport_Object | null; objectArray: Array; optObjectArray: Array | null; en: Types.TestImport_Enum; - optEnum: Option; + optEnum: Box | null; enumArray: Array; - optEnumArray: Array> | null; + optEnumArray: Array | null> | null; } export function serializeimportedMethodArgs(args: Args_importedMethod): ArrayBuffer { @@ -56,14 +56,14 @@ export function writeimportedMethodArgs( writer.writeString("u"); writer.writeUInt32(args.u); writer.context().pop(); - writer.context().push("optU", "Option", "writing property"); + writer.context().push("optU", "Box | null", "writing property"); writer.writeString("optU"); writer.writeOptionalUInt32(args.optU); writer.context().pop(); - writer.context().push("uArrayArray", "Array> | null>", "writing property"); + writer.context().push("uArrayArray", "Array | null> | null>", "writing property"); writer.writeString("uArrayArray"); - writer.writeArray(args.uArrayArray, (writer: Write, item: Array> | null): void => { - writer.writeOptionalArray(item, (writer: Write, item: Option): void => { + writer.writeArray(args.uArrayArray, (writer: Write, item: Array | null> | null): void => { + writer.writeOptionalArray(item, (writer: Write, item: Box | null): void => { writer.writeOptionalUInt32(item); }); }); @@ -100,7 +100,7 @@ export function writeimportedMethodArgs( writer.writeString("en"); writer.writeInt32(args.en); writer.context().pop(); - writer.context().push("optEnum", "Option", "writing property"); + writer.context().push("optEnum", "Box | null", "writing property"); writer.writeString("optEnum"); writer.writeOptionalInt32(args.optEnum); writer.context().pop(); @@ -110,9 +110,9 @@ export function writeimportedMethodArgs( writer.writeInt32(item); }); writer.context().pop(); - writer.context().push("optEnumArray", "Array> | null", "writing property"); + writer.context().push("optEnumArray", "Array | null> | null", "writing property"); writer.writeString("optEnumArray"); - writer.writeOptionalArray(args.optEnumArray, (writer: Write, item: Option): void => { + writer.writeOptionalArray(args.optEnumArray, (writer: Write, item: Box | null): void => { writer.writeOptionalInt32(item); }); writer.context().pop(); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/index.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/index.ts index f15d8a58de..984be2af32 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/index.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/index.ts @@ -1,7 +1,7 @@ import { Read, Write, - Option, + Box, BigInt, BigNumber, JSON @@ -23,9 +23,9 @@ export class TestImport_Object { objectArray: Array; optObjectArray: Array | null; en: Types.TestImport_Enum; - optEnum: Option; + optEnum: Box | null; enumArray: Array; - optEnumArray: Array> | null; + optEnumArray: Array | null> | null; static toBuffer(type: TestImport_Object): ArrayBuffer { return serializeTestImport_Object(type); diff --git a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/serialization.ts b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/serialization.ts index 423b9b892b..b243ba964a 100644 --- a/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/serialization.ts +++ b/packages/test-cases/cases/bind/sanity/output/wasm-as/imported/TestImport_Object/serialization.ts @@ -4,7 +4,7 @@ import { Write, WriteSizer, WriteEncoder, - Option, + Box, BigInt, BigNumber, JSON, @@ -58,7 +58,7 @@ export function writeTestImport_Object(writer: Write, type: TestImport_Object): writer.writeString("en"); writer.writeInt32(type.en); writer.context().pop(); - writer.context().push("optEnum", "Option", "writing property"); + writer.context().push("optEnum", "Box | null", "writing property"); writer.writeString("optEnum"); writer.writeOptionalInt32(type.optEnum); writer.context().pop(); @@ -68,9 +68,9 @@ export function writeTestImport_Object(writer: Write, type: TestImport_Object): writer.writeInt32(item); }); writer.context().pop(); - writer.context().push("optEnumArray", "Array> | null", "writing property"); + writer.context().push("optEnumArray", "Array | null> | null", "writing property"); writer.writeString("optEnumArray"); - writer.writeOptionalArray(type.optEnumArray, (writer: Write, item: Option): void => { + writer.writeOptionalArray(type.optEnumArray, (writer: Write, item: Box | null): void => { writer.writeOptionalInt32(item); }); writer.context().pop(); @@ -93,10 +93,10 @@ export function readTestImport_Object(reader: Read): TestImport_Object { let _optObjectArray: Array | null = null; let _en: Types.TestImport_Enum = 0; let _enSet: bool = false; - let _optEnum: Option = Option.None(); + let _optEnum: Box | null = null; let _enumArray: Array = []; let _enumArraySet: bool = false; - let _optEnumArray: Array> | null = null; + let _optEnumArray: Array | null> | null = null; while (numFields > 0) { numFields--; @@ -153,21 +153,21 @@ export function readTestImport_Object(reader: Read): TestImport_Object { reader.context().pop(); } else if (field == "optEnum") { - reader.context().push(field, "Option", "type found, reading property"); - let value: Option; + reader.context().push(field, "Box | null", "type found, reading property"); + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getTestImport_EnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeTestImport_EnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } _optEnum = value; reader.context().pop(); @@ -188,22 +188,22 @@ export function readTestImport_Object(reader: Read): TestImport_Object { reader.context().pop(); } else if (field == "optEnumArray") { - reader.context().push(field, "Array> | null", "type found, reading property"); - _optEnumArray = reader.readOptionalArray((reader: Read): Option => { - let value: Option; + reader.context().push(field, "Array | null> | null", "type found, reading property"); + _optEnumArray = reader.readOptionalArray((reader: Read): Box | null => { + let value: Box | null; if (!reader.isNextNil()) { if (reader.isNextString()) { - value = Option.Some( + value = Box.from( Types.getTestImport_EnumValue(reader.readString()) ); } else { - value = Option.Some( + value = Box.from( reader.readInt32() ); Types.sanitizeTestImport_EnumValue(value.unwrap()); } } else { - value = Option.None(); + value = null; } return value; }); diff --git a/packages/test-cases/cases/wrappers/wasm-as/reserved-words/src/index.ts b/packages/test-cases/cases/wrappers/wasm-as/reserved-words/src/index.ts index 6ea15ddd4b..dd620c6443 100644 --- a/packages/test-cases/cases/wrappers/wasm-as/reserved-words/src/index.ts +++ b/packages/test-cases/cases/wrappers/wasm-as/reserved-words/src/index.ts @@ -3,7 +3,7 @@ import { Args__for, _else, _while, - Box, + _Box, getwhileKey } from "./wrap"; @@ -13,7 +13,7 @@ export function _if(args: Args__if): _else { }; } -export function _for(args: Args__for): Box { +export function _for(args: Args__for): _Box { const value: _while = args._in; return { box: getwhileKey(value) diff --git a/packages/wasm/as/assembly/__tests__/msgpack.spec.ts b/packages/wasm/as/assembly/__tests__/msgpack.spec.ts index b464b41c99..76abba7f2c 100644 --- a/packages/wasm/as/assembly/__tests__/msgpack.spec.ts +++ b/packages/wasm/as/assembly/__tests__/msgpack.spec.ts @@ -6,11 +6,11 @@ import { Write, WriteEncoder, WriteSizer, + Box, JSON, BigInt, BigNumber, } from "../"; -import { Option } from "as-container"; class Sanity { nil: string | null = "null"; @@ -21,8 +21,8 @@ class Sanity { uint16: u16; uint32: u32; boolean: bool; - optUint32: Option = Option.None(); - optBool: Option = Option.None(); + optUint32: Box | null = null; + optBool: Box | null = null; float32: f32; float64: f64; str: string = ""; @@ -49,8 +49,8 @@ class Sanity { this.uint16 = 65535; this.uint32 = 4294967295; this.boolean = true; - this.optUint32 = Option.Some(234234234); - this.optBool = Option.Some(true); + this.optUint32 = Box.from(234234234); + this.optBool = Box.from(true); this.float32 = 3.40282344818115234375; this.float64 = 3124124512.598273468017578125; this.str = "Hello, world!"; diff --git a/packages/wasm/as/assembly/__tests__/msgpack_write_option.spec.ts b/packages/wasm/as/assembly/__tests__/msgpack_write_optional.spec.ts similarity index 91% rename from packages/wasm/as/assembly/__tests__/msgpack_write_option.spec.ts rename to packages/wasm/as/assembly/__tests__/msgpack_write_optional.spec.ts index 58ac494560..a866476f93 100644 --- a/packages/wasm/as/assembly/__tests__/msgpack_write_option.spec.ts +++ b/packages/wasm/as/assembly/__tests__/msgpack_write_optional.spec.ts @@ -3,7 +3,7 @@ import { WriteSizer } from "../msgpack/WriteSizer"; import { WriteEncoder } from "../msgpack/WriteEncoder"; import { BigInt, BigNumber } from "../math"; import { JSON } from "../json"; -import { Option } from "../"; +import { Box } from "../containers"; function fill(arr: Array): ArrayBuffer { const buffer = new ArrayBuffer(arr.length); @@ -26,12 +26,12 @@ class Case { } } -describe("WriteEncoder Option types", () => { +describe("WriteEncoder optional types", () => { it("TestWriteOptionalBool", () => { const cases = [ - new Case>("nil", Option.None(), [192]), - new Case>("nil", Option.Some(false), [194]), - new Case>("nil", Option.Some(true), [195]), + new Case | null>("nil", null, [192]), + new Case | null>("nil", Box.from(false), [194]), + new Case | null>("nil", Box.from(true), [195]), ]; for (let i: i32 = 0; i < cases.length; ++i) { @@ -50,10 +50,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalInt8", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "positive fixed int", - Option.Some(-128), + Box.from(-128), [208,128] ), ]; @@ -74,10 +74,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalInt16", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "16-bit signed int (negative)", - Option.Some(-32768), + Box.from(-32768), [209, 128, 0] ), ]; @@ -98,10 +98,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalInt32", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "32-bit signed int (negative)", - Option.Some(-32769), + Box.from(-32769), [210, 255, 255, 127, 255] ), ]; @@ -122,10 +122,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalUint8", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "8-bit unsigned int", - Option.Some(200), + Box.from(200), [204,200] ), ]; @@ -146,10 +146,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalUint16", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "16-bit unsigned int", - Option.Some(256), + Box.from(256), [205,1,0] ), ]; @@ -170,10 +170,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalUint32", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "32-bit unsigned int", - Option.Some(65536), + Box.from(65536), [206,0,1,0,0] ), ]; @@ -194,10 +194,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalFloat32", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "32-bit float", - Option.Some(0.5), + Box.from(0.5), [202,63,0,0,0] ), ]; @@ -218,10 +218,10 @@ describe("WriteEncoder Option types", () => { it("TestWriteOptionalFloat64", () => { const cases = [ - new Case>("none", Option.None(), [192]), - new Case>( + new Case | null>("none", null, [192]), + new Case | null>( "64-bit float", - Option.Some(3.141592653589793), + Box.from(3.141592653589793), [203, 64, 9, 33, 251, 84, 68, 45, 24] ), ]; diff --git a/packages/wasm/as/assembly/containers/index.ts b/packages/wasm/as/assembly/containers/index.ts index 24b9e120e7..219d8c2dff 100644 --- a/packages/wasm/as/assembly/containers/index.ts +++ b/packages/wasm/as/assembly/containers/index.ts @@ -1 +1,2 @@ export * from "./Result"; +export { Box } from "as-container"; diff --git a/packages/wasm/as/assembly/index.ts b/packages/wasm/as/assembly/index.ts index 372b974bd4..38900a8a8d 100644 --- a/packages/wasm/as/assembly/index.ts +++ b/packages/wasm/as/assembly/index.ts @@ -1,4 +1,3 @@ -export { Option } from "as-container"; export { Read, ReadDecoder, Write, WriteEncoder, WriteSizer } from "./msgpack"; export * from "./json"; export * from "./math"; diff --git a/packages/wasm/as/assembly/msgpack/Read.ts b/packages/wasm/as/assembly/msgpack/Read.ts index 6237209a0e..1d3fd77a0f 100644 --- a/packages/wasm/as/assembly/msgpack/Read.ts +++ b/packages/wasm/as/assembly/msgpack/Read.ts @@ -1,8 +1,7 @@ import { BigInt, BigNumber } from "../math"; import { Context } from "../debug"; import { JSON } from "../json"; - -import { Option } from "as-container"; +import { Box } from "../containers"; export abstract class Read { abstract readBool(): bool; @@ -33,15 +32,15 @@ export abstract class Read { value_fn: (reader: Read) => V ): Map; - abstract readOptionalBool(): Option; - abstract readOptionalInt8(): Option; - abstract readOptionalInt16(): Option; - abstract readOptionalInt32(): Option; - abstract readOptionalUInt8(): Option; - abstract readOptionalUInt16(): Option; - abstract readOptionalUInt32(): Option; - abstract readOptionalFloat32(): Option; - abstract readOptionalFloat64(): Option; + abstract readOptionalBool(): Box | null; + abstract readOptionalInt8(): Box | null; + abstract readOptionalInt16(): Box | null; + abstract readOptionalInt32(): Box | null; + abstract readOptionalUInt8(): Box | null; + abstract readOptionalUInt16(): Box | null; + abstract readOptionalUInt32(): Box | null; + abstract readOptionalFloat32(): Box | null; + abstract readOptionalFloat64(): Box | null; abstract readOptionalString(): string | null; abstract readOptionalBytes(): ArrayBuffer | null; abstract readOptionalBigInt(): BigInt | null; diff --git a/packages/wasm/as/assembly/msgpack/ReadDecoder.ts b/packages/wasm/as/assembly/msgpack/ReadDecoder.ts index 05c4dd4473..58a3529d1d 100644 --- a/packages/wasm/as/assembly/msgpack/ReadDecoder.ts +++ b/packages/wasm/as/assembly/msgpack/ReadDecoder.ts @@ -14,8 +14,7 @@ import { BigInt, BigNumber } from "../math"; import { Context } from "../debug"; import { JSON } from "../json"; import { ExtensionType } from "./ExtensionType"; - -import { Option } from "as-container"; +import { Box } from "../containers"; export class ReadDecoder extends Read { private readonly _context: Context; @@ -364,67 +363,67 @@ export class ReadDecoder extends Read { return this.readMap(key_fn, value_fn); } - readOptionalBool(): Option { + readOptionalBool(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readBool()); + return Box.from(this.readBool()); } - readOptionalInt8(): Option { + readOptionalInt8(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readInt8()); + return Box.from(this.readInt8()); } - readOptionalInt16(): Option { + readOptionalInt16(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readInt16()); + return Box.from(this.readInt16()); } - readOptionalInt32(): Option { + readOptionalInt32(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readInt32()); + return Box.from(this.readInt32()); } - readOptionalUInt8(): Option { + readOptionalUInt8(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readUInt8()); + return Box.from(this.readUInt8()); } - readOptionalUInt16(): Option { + readOptionalUInt16(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readUInt16()); + return Box.from(this.readUInt16()); } - readOptionalUInt32(): Option { + readOptionalUInt32(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readUInt32()); + return Box.from(this.readUInt32()); } - readOptionalFloat32(): Option { + readOptionalFloat32(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readFloat32()); + return Box.from(this.readFloat32()); } - readOptionalFloat64(): Option { + readOptionalFloat64(): Box | null { if (this.isNextNil()) { - return Option.None(); + return null; } - return Option.Some(this.readFloat64()); + return Box.from(this.readFloat64()); } readOptionalString(): string | null { diff --git a/packages/wasm/as/assembly/msgpack/Write.ts b/packages/wasm/as/assembly/msgpack/Write.ts index 843a2ead8b..7f6e07d37d 100644 --- a/packages/wasm/as/assembly/msgpack/Write.ts +++ b/packages/wasm/as/assembly/msgpack/Write.ts @@ -1,8 +1,7 @@ import { BigInt, BigNumber } from "../math"; import { Context } from "../debug"; import { JSON } from "../json"; - -import { Option } from "as-container"; +import { Box } from "../containers"; export abstract class Write { abstract writeNil(): void; @@ -39,15 +38,15 @@ export abstract class Write { value_fn: (writer: Write, value: V) => void ): void; - abstract writeOptionalBool(value: Option): void; - abstract writeOptionalInt8(value: Option): void; - abstract writeOptionalInt16(value: Option): void; - abstract writeOptionalInt32(value: Option): void; - abstract writeOptionalUInt8(value: Option): void; - abstract writeOptionalUInt16(value: Option): void; - abstract writeOptionalUInt32(value: Option): void; - abstract writeOptionalFloat32(value: Option): void; - abstract writeOptionalFloat64(value: Option): void; + abstract writeOptionalBool(value: Box | null): void; + abstract writeOptionalInt8(value: Box | null): void; + abstract writeOptionalInt16(value: Box | null): void; + abstract writeOptionalInt32(value: Box | null): void; + abstract writeOptionalUInt8(value: Box | null): void; + abstract writeOptionalUInt16(value: Box | null): void; + abstract writeOptionalUInt32(value: Box | null): void; + abstract writeOptionalFloat32(value: Box | null): void; + abstract writeOptionalFloat64(value: Box | null): void; abstract writeOptionalString(value: string | null): void; abstract writeOptionalBytes(value: ArrayBuffer | null): void; abstract writeOptionalBigInt(value: BigInt | null): void; diff --git a/packages/wasm/as/assembly/msgpack/WriteEncoder.ts b/packages/wasm/as/assembly/msgpack/WriteEncoder.ts index 0894adefa9..abd58d6bcb 100644 --- a/packages/wasm/as/assembly/msgpack/WriteEncoder.ts +++ b/packages/wasm/as/assembly/msgpack/WriteEncoder.ts @@ -6,8 +6,7 @@ import { Write } from "./Write"; import { BigInt, BigNumber } from "../math"; import { Context } from "../debug"; import { JSON } from "../json"; - -import { Option } from "as-container"; +import { Box } from "../containers"; export class WriteEncoder extends Write { private readonly _context: Context; @@ -240,8 +239,8 @@ export class WriteEncoder extends Write { this.writeMap(m, key_fn, value_fn); } - writeOptionalBool(value: Option): void { - if (value.isNone) { + writeOptionalBool(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -249,8 +248,8 @@ export class WriteEncoder extends Write { this.writeBool(value.unwrap()); } - writeOptionalInt8(value: Option): void { - if (value.isNone) { + writeOptionalInt8(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -258,8 +257,8 @@ export class WriteEncoder extends Write { this.writeInt8(value.unwrap()); } - writeOptionalInt16(value: Option): void { - if (value.isNone) { + writeOptionalInt16(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -267,8 +266,8 @@ export class WriteEncoder extends Write { this.writeInt16(value.unwrap()); } - writeOptionalInt32(value: Option): void { - if (value.isNone) { + writeOptionalInt32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -276,8 +275,8 @@ export class WriteEncoder extends Write { this.writeInt32(value.unwrap()); } - writeOptionalUInt8(value: Option): void { - if (value.isNone) { + writeOptionalUInt8(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -285,8 +284,8 @@ export class WriteEncoder extends Write { this.writeUInt8(value.unwrap()); } - writeOptionalUInt16(value: Option): void { - if (value.isNone) { + writeOptionalUInt16(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -294,8 +293,8 @@ export class WriteEncoder extends Write { this.writeUInt16(value.unwrap()); } - writeOptionalUInt32(value: Option): void { - if (value.isNone) { + writeOptionalUInt32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -303,8 +302,8 @@ export class WriteEncoder extends Write { this.writeUInt32(value.unwrap()); } - writeOptionalFloat32(value: Option): void { - if (value.isNone) { + writeOptionalFloat32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -312,8 +311,8 @@ export class WriteEncoder extends Write { this.writeFloat32(value.unwrap()); } - writeOptionalFloat64(value: Option): void { - if (value.isNone) { + writeOptionalFloat64(value: Box | null): void { + if (value === null) { this.writeNil(); return; } diff --git a/packages/wasm/as/assembly/msgpack/WriteSizer.ts b/packages/wasm/as/assembly/msgpack/WriteSizer.ts index a02eb4b8f1..07f2f8fd9e 100644 --- a/packages/wasm/as/assembly/msgpack/WriteSizer.ts +++ b/packages/wasm/as/assembly/msgpack/WriteSizer.ts @@ -2,8 +2,7 @@ import { Write } from "./Write"; import { BigInt, BigNumber } from "../math"; import { Context } from "../debug"; import { JSON } from "../json"; - -import { Option } from "as-container"; +import { Box } from "../containers"; export class WriteSizer extends Write { length: i32; @@ -199,8 +198,8 @@ export class WriteSizer extends Write { this.extByteLengths[extIdx] = byteLength; } - writeOptionalBool(value: Option): void { - if (value.isNone) { + writeOptionalBool(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -208,8 +207,8 @@ export class WriteSizer extends Write { this.writeBool(value.unwrap()); } - writeOptionalInt8(value: Option): void { - if (value.isNone) { + writeOptionalInt8(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -217,8 +216,8 @@ export class WriteSizer extends Write { this.writeInt8(value.unwrap()); } - writeOptionalInt16(value: Option): void { - if (value.isNone) { + writeOptionalInt16(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -226,8 +225,8 @@ export class WriteSizer extends Write { this.writeInt16(value.unwrap()); } - writeOptionalInt32(value: Option): void { - if (value.isNone) { + writeOptionalInt32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -235,8 +234,8 @@ export class WriteSizer extends Write { this.writeInt32(value.unwrap()); } - writeOptionalUInt8(value: Option): void { - if (value.isNone) { + writeOptionalUInt8(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -244,8 +243,8 @@ export class WriteSizer extends Write { this.writeUInt8(value.unwrap()); } - writeOptionalUInt16(value: Option): void { - if (value.isNone) { + writeOptionalUInt16(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -253,8 +252,8 @@ export class WriteSizer extends Write { this.writeUInt16(value.unwrap()); } - writeOptionalUInt32(value: Option): void { - if (value.isNone) { + writeOptionalUInt32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -262,8 +261,8 @@ export class WriteSizer extends Write { this.writeUInt32(value.unwrap()); } - writeOptionalFloat32(value: Option): void { - if (value.isNone) { + writeOptionalFloat32(value: Box | null): void { + if (value === null) { this.writeNil(); return; } @@ -271,8 +270,8 @@ export class WriteSizer extends Write { this.writeFloat32(value.unwrap()); } - writeOptionalFloat64(value: Option): void { - if (value.isNone) { + writeOptionalFloat64(value: Box | null): void { + if (value === null) { this.writeNil(); return; }