-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasm: Provide toWallyMap/fromWallyMap utility functions
- Loading branch information
1 parent
59a4cde
commit 8525f7b
Showing
4 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './core.js' | ||
export * from './const.js' | ||
export * from './functions.js' | ||
export * from './functions.js' | ||
export * from './util.js' |
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,36 @@ | ||
import { map_get_num_items, map_get_item_key, map_get_item_key_length, map_get_item_integer_key, map_get_item, map_init, map_add, map_add_integer } from "./functions.js" | ||
|
||
export function fromWallyMap(wally_map) { | ||
const js_map = new Map, map_len = map_get_num_items(wally_map) | ||
|
||
for (let i = 0; i < map_len; i++) { | ||
const is_int_key = map_get_item_key_length(wally_map, i) == 0 | ||
, key = is_int_key ? map_get_item_integer_key(wally_map, i) | ||
: map_get_item_key(wally_map, i).toString() | ||
js_map.set(key, map_get_item(wally_map, i)) | ||
} | ||
|
||
return js_map | ||
} | ||
|
||
export function toWallyMap(js_map) { | ||
if (js_map && js_map.constructor == Object) { | ||
// Convert plain objects into a Map | ||
js_map = new Map(Object.entries(js_map)) | ||
} else if (!(js_map instanceof Map)) { | ||
throw new Error('Invalid map for toWallyMap') | ||
} | ||
|
||
const wally_map = map_init(js_map.size, null) | ||
|
||
js_map.forEach((val, key) => { | ||
val = Buffer.from(val) | ||
if (typeof key == 'number') { | ||
map_add_integer(wally_map, key, val) | ||
} else { | ||
map_add(wally_map, Buffer.from(key), val) | ||
} | ||
}) | ||
|
||
return wally_map | ||
} |