Skip to content

Commit dccd975

Browse files
authored
literal map (#107)
1 parent ace92f0 commit dccd975

File tree

8 files changed

+510
-2
lines changed

8 files changed

+510
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(PREJS_FILES
3737
${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/dynload/dynload.js
3838
${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/fetch.js
3939
${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/load_pkg.js
40+
${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/pre_js/literal_map.js
4041
)
4142
set(POSTJS_FILES
4243
${CMAKE_CURRENT_SOURCE_DIR}/include/pyjs/post_js/fixes.js

build_debug2.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# dir of this script
5+
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
PREFIX_DIR=$THIS_DIR/wasm_prefix
7+
EMSDK_DIR=$THIS_DIR/emsdk_install
8+
EMSDK_VERSION="3.1.58"
9+
10+
if [ ! -d "$EMSDK_DIR" ]; then
11+
echo "Creating emsdk dir $EMSDK_DIR"
12+
$THIS_DIR/emsdk/setup_emsdk.sh $EMSDK_VERSION $EMSDK_DIR
13+
else
14+
echo "Emsdk dir $EMSDK_DIR already exists"
15+
fi
16+
17+
18+
if true; then
19+
rm -rf $PREFIX_DIR
20+
echo "Creating wasm env at $PREFIX_DIR"
21+
$MAMBA_EXE create -p $PREFIX_DIR \
22+
--platform=emscripten-wasm32 \
23+
-c https://repo.mamba.pm/emscripten-forge \
24+
-c https://repo.mamba.pm/conda-forge \
25+
--yes \
26+
python pybind11 "emscripten-abi=3.1.58"\
27+
bzip2 sqlite zlib libffi pytest
28+
fi
29+
30+
cd $THIS_DIR
31+
source $EMSDK_DIR/emsdk_env.sh
32+
33+
34+
mkdir -p build
35+
cd build
36+
37+
export PREFIX=$PREFIX_DIR
38+
export CMAKE_PREFIX_PATH=$PREFIX
39+
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
40+
41+
emcmake cmake \
42+
-DCMAKE_BUILD_TYPE=Release \
43+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
44+
-DBUILD_RUNTIME_BROWSER=ON \
45+
-DBUILD_RUNTIME_NODE=OFF \
46+
-DCMAKE_INSTALL_PREFIX=$PREFIX \
47+
..
48+
49+
emmake make -j8
50+
emmake make install

include/pyjs/pre_js/literal_map.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// see literal_map_license.txt for license information
2+
3+
const { get, getOwnPropertyDescriptor, ownKeys } = Reflect;
4+
5+
const getPropertyDescriptor = (value) => ({
6+
value,
7+
enumerable: true,
8+
writable: true,
9+
configurable: true,
10+
});
11+
12+
const _ = Symbol();
13+
const prototype = "prototype";
14+
15+
const handler = {
16+
deleteProperty: (map, k) => (map.has(k) ? map.delete(k) : delete map[k]),
17+
get(map, k, proxy) {
18+
if (k === _) return map;
19+
let v = map[k];
20+
if (typeof v === "function" && k !== "constructor") {
21+
v = v.bind(map);
22+
}
23+
v ||= map.get(k);
24+
return v;
25+
},
26+
getOwnPropertyDescriptor(map, k) {
27+
if (map.has(k)) return getPropertyDescriptor(map.get(k));
28+
if (k in map) return getOwnPropertyDescriptor(map, k);
29+
},
30+
has: (map, k) => map.has(k) || k in map,
31+
ownKeys: (map) =>
32+
[...map.keys(), ...ownKeys(map)].filter((x) =>
33+
["string", "symbol"].includes(typeof x),
34+
),
35+
set: (map, k, v) => (map.set(k, v), true),
36+
};
37+
38+
const LiteralMap = new Proxy(
39+
class LiteralMap extends Map {
40+
constructor(...args) {
41+
return new Proxy(super(...args), handler);
42+
}
43+
},
44+
{
45+
get(Class, k, ...rest) {
46+
return k !== prototype && k in Class[prototype]
47+
? (proxy, ...args) => {
48+
const map = proxy[_];
49+
let value = map[k];
50+
if (typeof value === "function") value = value.apply(map, args);
51+
// prevent leaking the internal map elsewhere
52+
return value === map ? proxy : value;
53+
}
54+
: get(Class, k, ...rest);
55+
},
56+
},
57+
);
58+
59+
// export default LiteralMap;
60+
globalThis.LiteralMap = LiteralMap;

0 commit comments

Comments
 (0)