Allow access to Node's TextDecoder
-
Re: Allow access to node's Buffer class
I'm working on getting Screeps to work with Rust's
wasm-bindgen
(the official way to run Rust in Node.js). The only thing blocking this is that the Rust/JS shim makes use ofTextDecoder
for performance. This API has existing in Node since Node 8 (screeps uses Node 10) and is also supported in most major browsers, so it would also work with the simulator.In fact,
TextDecoder
is already available in the simulator at global scope, so Rust already works there. We just needTextDecoder
to also be there when running in Node.Previous discussions mentioned allowing access to Node's
Buffer
class, but that's no longer necessary as the Rust Shims now just useTextDecoder
andUint8Array
.This came up because the toolchain that the existing Rust API uses (
stdweb
) is no longer maintained, and thewasm-bindgen
alternative is officially supported by the Rust org.
-
Github Issues:
- https://github.com/rustyscreeps/screeps-game-api/issues/329
- https://github.com/screeps/screeps/issues/120
Relevant APIs:
-
I was slightly wrong above, we could also solve this problem for Rust by exposing the
Buffer
class. However, as noted in the previous linked pull request, that requires a unnecessary polyfill for Browsers.I think the best approach is to just expose
TextDecoder
/TextEncoder
either at global scope or in arequire('util')
module.
-
As I understand it, Node's Buffer is dangerous to expose from a security perspective. This is because it allows you to allocate and access uninitialised memory... though perhaps in the context of IVM this is cleared? Anyway, at the very least it's a bunch of pointer leaks they probably don't want.
Back to your original idea, providing TextEncoder and TextDecoder would be really useful not just for Rust users, but for anyone wanting to do custom memory or WASM modules. I'm in the JS crowd, but would love to have a proper way to store an ArrayBuffer in RawMemory or segments without an incredibly slow polyfill. JavaScript strings are just awful to deal with.