Skip to content

Commit

Permalink
Merge pull request #105 from Tim-Paik/master
Browse files Browse the repository at this point in the history
fix compile errors
  • Loading branch information
tamasfe committed Oct 25, 2022
2 parents 02c655a + 692a1bc commit 202ebcf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/rhai-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ glob = "0.3.0"
atty = "0.2.14"

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.19.2" }
tokio = { version = "1.19.2", features = ["io-util"]}
2 changes: 1 addition & 1 deletion crates/rhai-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ glob = "0.3.0"
globset = "0.4.9"
indexmap = "1.9.1"
itertools = "0.10.3"
lsp-async-stub = { version = "0.6.0", features = ["tokio-stdio"] }
lsp-async-stub = "0.6.0"
lsp-types = "0.93.0"
once_cell = "1.12.0"
serde = { version = "1.0.137", features = ["derive"] }
Expand Down
16 changes: 16 additions & 0 deletions crates/rhai-wasm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub(crate) struct WasmEnvironment {
js_on_stderr: Function,
js_glob_files: Function,
js_read_file: Function,
js_write_file: Function,
js_sleep: Function,
js_is_absolute: Function,
js_cwd: Function,
Expand Down Expand Up @@ -203,6 +204,9 @@ impl From<JsValue> for WasmEnvironment {
js_read_file: js_sys::Reflect::get(&val, &JsValue::from_str("js_read_file"))
.unwrap()
.into(),
js_write_file: js_sys::Reflect::get(&val, &JsValue::from_str("js_write_file"))
.unwrap()
.into(),
js_is_absolute: js_sys::Reflect::get(&val, &JsValue::from_str("js_is_absolute"))
.unwrap()
.into(),
Expand Down Expand Up @@ -308,6 +312,18 @@ impl Environment for WasmEnvironment {
Ok(Uint8Array::from(ret).to_vec())
}

async fn write_file(&self, path: &Path, bytes: &[u8]) -> Result<(), anyhow::Error> {
let path_str = JsValue::from_str(&path.to_string_lossy());
let this = JsValue::null();
let data = JsValue::from(js_sys::Uint8Array::from(bytes));
let res: JsValue = self.js_write_file.call2(&this, &path_str, &data).unwrap();

JsFuture::from(Promise::from(res))
.await
.map(|_| ())
.map_err(|err| anyhow!("{:?}", err))
}

fn is_absolute(&self, path: &Path) -> bool {
let path_str = JsValue::from_str(&path.to_string_lossy());
let this = JsValue::null();
Expand Down
1 change: 1 addition & 0 deletions editors/vscode/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ process.on("message", async (d: RpcMessage) => {
glob: p => glob.sync(p),
isAbsolute: p => path.isAbsolute(p),
readFile: path => fsPromise.readFile(path),
writeFile: (path, data) => fsPromise.writeFile(path, data),
stderr: process.stderr,
stdErrAtty: () => process.stderr.isTTY,
stdin: process.stdin,
Expand Down
5 changes: 5 additions & 0 deletions js/core/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export interface Environment {
* Read a file at the given path.
*/
readFile: (path: string) => Promise<Uint8Array>;
/**
* Write a file at the given path.
*/
writeFile: (path: string, data: Uint8Array) => Promise<void>;
/**
* Search a glob file pattern and return the matched files.
*/
Expand Down Expand Up @@ -93,6 +97,7 @@ export function convertEnv(env: Environment): any {
js_url_to_file_path: env.urlToFilePath,
js_sleep: env.sleep,
js_read_file: env.readFile,
js_write_file: env.writeFile,
};
}

Expand Down

0 comments on commit 202ebcf

Please sign in to comment.