-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add YAML parsing support to WASM runtime (#5785)
* runtime: add yaml parsing support to wasm runtime * runtime: add yaml parsing tests
- Loading branch information
Showing
15 changed files
with
533 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import "allocator/arena"; | ||
|
||
import {Bytes, Result} from "../api_version_0_0_5/common/types"; | ||
import {debug, YAMLValue} from "../api_version_0_0_5/common/yaml"; | ||
|
||
export {memory}; | ||
|
||
declare namespace yaml { | ||
function try_fromBytes(data: Bytes): Result<YAMLValue, boolean>; | ||
} | ||
|
||
export function handleYaml(data: Bytes): string { | ||
let result = yaml.try_fromBytes(data); | ||
|
||
if (result.isError) { | ||
return "error"; | ||
} | ||
|
||
return debug(result.value); | ||
} |
Binary file not shown.
139 changes: 139 additions & 0 deletions
139
runtime/test/wasm_test/api_version_0_0_5/common/yaml.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import {TypedMap} from './types'; | ||
|
||
export enum YAMLValueKind { | ||
NULL = 0, | ||
BOOL = 1, | ||
NUMBER = 2, | ||
STRING = 3, | ||
ARRAY = 4, | ||
OBJECT = 5, | ||
TAGGED = 6, | ||
} | ||
|
||
export class YAMLValue { | ||
kind: YAMLValueKind; | ||
data: u64; | ||
|
||
isBool(): boolean { | ||
return this.kind == YAMLValueKind.BOOL; | ||
} | ||
|
||
isNumber(): boolean { | ||
return this.kind == YAMLValueKind.NUMBER; | ||
} | ||
|
||
isString(): boolean { | ||
return this.kind == YAMLValueKind.STRING; | ||
} | ||
|
||
isArray(): boolean { | ||
return this.kind == YAMLValueKind.ARRAY; | ||
} | ||
|
||
isObject(): boolean { | ||
return this.kind == YAMLValueKind.OBJECT; | ||
} | ||
|
||
isTagged(): boolean { | ||
return this.kind == YAMLValueKind.TAGGED; | ||
} | ||
|
||
|
||
toBool(): boolean { | ||
assert(this.isBool(), 'YAML value is not a boolean'); | ||
return this.data != 0; | ||
} | ||
|
||
toNumber(): string { | ||
assert(this.isNumber(), 'YAML value is not a number'); | ||
return changetype<string>(this.data as usize); | ||
} | ||
|
||
toString(): string { | ||
assert(this.isString(), 'YAML value is not a string'); | ||
return changetype<string>(this.data as usize); | ||
} | ||
|
||
toArray(): Array<YAMLValue> { | ||
assert(this.isArray(), 'YAML value is not an array'); | ||
return changetype<Array<YAMLValue>>(this.data as usize); | ||
} | ||
|
||
toObject(): TypedMap<YAMLValue, YAMLValue> { | ||
assert(this.isObject(), 'YAML value is not an object'); | ||
return changetype<TypedMap<YAMLValue, YAMLValue>>(this.data as usize); | ||
} | ||
|
||
toTagged(): YAMLTaggedValue { | ||
assert(this.isTagged(), 'YAML value is not tagged'); | ||
return changetype<YAMLTaggedValue>(this.data as usize); | ||
} | ||
} | ||
|
||
export class YAMLTaggedValue { | ||
tag: string; | ||
value: YAMLValue; | ||
} | ||
|
||
|
||
export function debug(value: YAMLValue): string { | ||
return "(" + value.kind.toString() + ") " + debug_value(value); | ||
} | ||
|
||
function debug_value(value: YAMLValue): string { | ||
switch (value.kind) { | ||
case YAMLValueKind.NULL: | ||
return "null"; | ||
case YAMLValueKind.BOOL: | ||
return value.toBool() ? "true" : "false"; | ||
case YAMLValueKind.NUMBER: | ||
return value.toNumber(); | ||
case YAMLValueKind.STRING: | ||
return value.toString(); | ||
case YAMLValueKind.ARRAY: { | ||
let arr = value.toArray(); | ||
|
||
let s = "["; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (i > 0) { | ||
s += ", "; | ||
} | ||
s += debug(arr[i]); | ||
} | ||
s += "]"; | ||
|
||
return s; | ||
} | ||
case YAMLValueKind.OBJECT: { | ||
let arr = value.toObject().entries.sort((a, b) => { | ||
if (a.key.toString() < b.key.toString()) { | ||
return -1; | ||
} | ||
|
||
if (a.key.toString() > b.key.toString()) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
let s = "{"; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (i > 0) { | ||
s += ", "; | ||
} | ||
s += debug_value(arr[i].key) + ": " + debug(arr[i].value); | ||
} | ||
s += "}"; | ||
|
||
return s; | ||
} | ||
case YAMLValueKind.TAGGED: { | ||
let tagged = value.toTagged(); | ||
|
||
return tagged.tag + " " + debug(tagged.value); | ||
} | ||
default: | ||
return "undefined"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {debug, YAMLValue, YAMLTaggedValue} from './common/yaml'; | ||
import {Bytes, Result, TypedMap, TypedMapEntry, Wrapped} from './common/types'; | ||
|
||
enum TypeId { | ||
STRING = 0, | ||
UINT8_ARRAY = 6, | ||
|
||
YamlValue = 5500, | ||
YamlTaggedValue = 5501, | ||
YamlTypedMapEntryValueValue = 5502, | ||
YamlTypedMapValueValue = 5503, | ||
YamlArrayValue = 5504, | ||
YamlArrayTypedMapEntryValueValue = 5505, | ||
YamlWrappedValue = 5506, | ||
YamlResultValueBool = 5507, | ||
} | ||
|
||
export function id_of_type(type_id_index: TypeId): usize { | ||
switch (type_id_index) { | ||
case TypeId.STRING: | ||
return idof<string>(); | ||
case TypeId.UINT8_ARRAY: | ||
return idof<Uint8Array>(); | ||
|
||
case TypeId.YamlValue: | ||
return idof<YAMLValue>(); | ||
case TypeId.YamlTaggedValue: | ||
return idof<YAMLTaggedValue>(); | ||
case TypeId.YamlTypedMapEntryValueValue: | ||
return idof<TypedMapEntry<YAMLValue, YAMLValue>>(); | ||
case TypeId.YamlTypedMapValueValue: | ||
return idof<TypedMap<YAMLValue, YAMLValue>>(); | ||
case TypeId.YamlArrayValue: | ||
return idof<Array<YAMLValue>>(); | ||
case TypeId.YamlArrayTypedMapEntryValueValue: | ||
return idof<Array<TypedMapEntry<YAMLValue, YAMLValue>>>(); | ||
case TypeId.YamlWrappedValue: | ||
return idof<Wrapped<YAMLValue>>(); | ||
case TypeId.YamlResultValueBool: | ||
return idof<Result<YAMLValue, boolean>>(); | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
export function allocate(n: usize): usize { | ||
return __alloc(n); | ||
} | ||
|
||
declare namespace yaml { | ||
function try_fromBytes(data: Bytes): Result<YAMLValue, boolean>; | ||
} | ||
|
||
export function handleYaml(data: Bytes): string { | ||
let result = yaml.try_fromBytes(data); | ||
|
||
if (result.isError) { | ||
return "error"; | ||
} | ||
|
||
return debug(result.value); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.