Skip to content

Commit

Permalink
WIP Browser Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Aug 13, 2022
1 parent 1023afc commit a3d3da0
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 185 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/web-target
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ bevy = { version = "0.8.0", default-features = false, features = ["bevy_asset"]
bevy_reflect = "0.8.0-dev"
fixedbitset = "0.4"

deno_core = "0.146.0"
serde = "1.0"
serde_json = "1.0"

Expand All @@ -24,5 +23,16 @@ swc_ecma_transforms_typescript = { version = "0.145.1" }
swc_ecma_visit = { version = "0.76.3" }
swc_atoms = { version = "0.4.3" }

[profile.dev]
opt-level = 1

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
deno_core = "0.146.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm_mutex = "0.1.4"
js-sys = "0.3.59"
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }

[dev-dependencies]
bevy = { version = "0.8.0", default-features = false, features = ["render", "bevy_winit", "x11", "filesystem_watcher"] }
32 changes: 32 additions & 0 deletions run-example-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/env bash

#
# This script is usually run by the justfile
#

example="breakout"
target=wasm32-unknown-unknown
target_dir="web-target"

release_arg=""
build_kind="debug"
dist_dir="$target_dir/wasm-debug"

if [ "$is_release" == "release" ]; then
release_arg="--release"
build_kind="release"
dist_dir="$target_dir/wasm-release"
fi

export CARGO_TARGET_DIR=$target_dir

set -ex

cargo build --target $target --example $example $release_arg
rm -rf $dist_dir
mkdir -p $dist_dir
wasm-bindgen --out-dir $dist_dir --target web --no-typescript $target_dir/$target/$build_kind/examples/$example.wasm
cp wasm_resources/index.html $dist_dir/index.html
cp -r assets $dist_dir

basic-http-server -x $dist_dir
61 changes: 43 additions & 18 deletions src/runtime/js/ecs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

((window) => {

class ComponentId {
index;
}
Expand All @@ -13,56 +12,82 @@
}

class World {
get #rid() { return 0 }
get #rid() {
return 0;
}

toString() {
return Deno.core.opSync("op_world_tostring", this.rid);
return bevyModJsScriptingOpSync("op_world_tostring", this.rid);
}

get components() {
return Deno.core.opSync("op_world_components", this.rid);
return bevyModJsScriptingOpSync("op_world_components", this.rid);
}

get resources() {
return Deno.core.opSync("op_world_resources", this.rid);
return bevyModJsScriptingOpSync("op_world_resources", this.rid);
}

get entities() {
return Deno.core.opSync("op_world_entities", this.rid);
return bevyModJsScriptingOpSync("op_world_entities", this.rid);
}

query(descriptor) {
return Deno.core.opSync("op_world_query", this.rid, descriptor)
.map(({ entity, components }) => ({
entity,
components: components.map(wrapValueRef),
}));
return bevyModJsScriptingOpSync(
"op_world_query",
this.rid,
descriptor
).map(({ entity, components }) => ({
entity,
components: components.map(wrapValueRef),
}));
}
}


const VALUE_REF_GET_INNER = Symbol("value_ref_get_inner");
function wrapValueRef(valueRef) {
// leaf primitives
if (typeof valueRef !== "object") { return valueRef };
if (typeof valueRef !== "object") {
return valueRef;
}
const proxy = new Proxy(valueRef, {
ownKeys: (target) => {
return Deno.core.opSync("op_value_ref_keys", world.rid, target);
return bevyModJsScriptingOpSync(
"op_value_ref_keys",
world.rid,
target
);
},
get: (target, p, receiver) => {
switch (p) {
case VALUE_REF_GET_INNER:
return target;
case "toString":
return () => Deno.core.opSync("op_value_ref_to_string", world.rid, target);
return () =>
bevyModJsScriptingOpSync(
"op_value_ref_to_string",
world.rid,
target
);
default:
let valueRef = Deno.core.opSync("op_value_ref_get", world.rid, target, "." + p);
let valueRef = bevyModJsScriptingOpSync(
"op_value_ref_get",
world.rid,
target,
"." + p
);
return wrapValueRef(valueRef);
}
},
set: (target, p, value) => {
Deno.core.opSync("op_value_ref_set", world.rid, target, "." + p, value);
}
bevyModJsScriptingOpSync(
"op_value_ref_set",
world.rid,
target,
"." + p,
value
);
},
});
return proxy;
}
Expand Down
20 changes: 10 additions & 10 deletions src/runtime/js/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

((window) => {
window.trace = function (val) {
Deno.core.opSync("op_log", "trace", val);
}
bevyModJsScriptingOpSync("op_log", "trace", val);
};
window.debug = function (val) {
Deno.core.opSync("op_log", "debug", val)
}
bevyModJsScriptingOpSync("op_log", "debug", val);
};
window.info = function (val) {
Deno.core.opSync("op_log", "info", val)
}
bevyModJsScriptingOpSync("op_log", "info", val);
};
window.warn = function (val) {
Deno.core.opSync("op_log", "warn", val)
}
bevyModJsScriptingOpSync("op_log", "warn", val);
};
window.error = function (val) {
Deno.core.opSync("op_log", "error", val)
}
bevyModJsScriptingOpSync("op_log", "error", val);
};
})(globalThis);
2 changes: 1 addition & 1 deletion src/runtime/native/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,6 @@ pub fn extension() -> Extension {
op_value_ref_set::decl(),
op_value_ref_call::decl(),
])
.js(include_js_files!(prefix "bevy", "js/ecs.js",))
.js(include_js_files!(prefix "bevy", "../js/ecs.js",))
.build()
}
81 changes: 0 additions & 81 deletions src/runtime/native/js/ecs.js

This file was deleted.

52 changes: 0 additions & 52 deletions src/runtime/native/js/index.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/runtime/native/js/log.js

This file was deleted.

Loading

0 comments on commit a3d3da0

Please sign in to comment.