Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebAssembly prototype #451

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"rbx_binary",
"rbx_binary_wasm",
"rbx_dom_weak",
"rbx_reflector",
"rbx_reflection",
Expand Down
6 changes: 6 additions & 0 deletions rbx_binary_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
24 changes: 24 additions & 0 deletions rbx_binary_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "rbx-binary"
version = "0.1.0"
edition = "2018"
description = "WebAssembly build of rbx_binary for decoding and encoding binary Roblox files."
repository = "https://github.com/rojo-rbx/rbx-dom"
license = "MIT"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
serde-wasm-bindgen = "0.4"
tsify = "0.4.5"
wasm-bindgen = "0.2.84"

rbx_dom_weak = { version = "2.9.0", path = "../rbx_dom_weak" }
rbx_binary = { version = "0.7.7", path = "../rbx_binary" }

getrandom = { version = "0.2", features = ["js"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
14 changes: 14 additions & 0 deletions rbx_binary_wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# rbx_binary_wasm

🐉 Thar be dragons 🐉

## Prereqs
- [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/)
- [npm](https://docs.npmjs.com/getting-started)

## Building
Run `wasm-pack build`

## Usage
1. In the `pkg` directory run `npm pack` to package to a tarball.
2. `npm i tarball.tgz` in your project
24 changes: 24 additions & 0 deletions rbx_binary_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
mod utils;

use rbx_dom_weak::WeakDom;
use utils::set_panic_hook;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn decode_file(binary_file: Vec<u8>) -> WeakDom {
set_panic_hook();

let dom = rbx_binary::from_reader(&binary_file[..]).expect("Failed to deserialize binary file.");

dom
}

#[wasm_bindgen]
pub fn encode_file(dom: WeakDom) -> Vec<u8> {
let mut binary_file = Vec::new();

rbx_binary::to_writer(&mut binary_file, &dom, &[dom.root_ref()])
.expect("Failed to serialize from DOM");

binary_file
}
10 changes: 10 additions & 0 deletions rbx_binary_wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
3 changes: 3 additions & 0 deletions rbx_dom_weak/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ rbx_types = { version = "1.10.0", path = "../rbx_types", features = ["serde"] }

serde = "1.0.137"

tsify = "0.4.5"
wasm-bindgen = "0.2.84"

[dev-dependencies]
insta = { version = "1.14.1", features = ["yaml"] }
8 changes: 7 additions & 1 deletion rbx_dom_weak/src/dom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::{HashMap, HashSet, VecDeque};

use rbx_types::{Ref, UniqueId, Variant};
use serde::{Deserialize, Serialize};
use tsify::Tsify;

use crate::instance::{Instance, InstanceBuilder};

Expand All @@ -11,10 +13,14 @@ use crate::instance::{Instance, InstanceBuilder};
///
/// When constructing instances, you'll want to create [`InstanceBuilder`]
/// objects and insert them into the tree.
#[derive(Debug)]
#[derive(Tsify, Debug, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct WeakDom {
instances: HashMap<Ref, Instance>,

#[tsify(optional)]
root_ref: Ref,

unique_ids: HashSet<UniqueId>,
}

Expand Down
4 changes: 3 additions & 1 deletion rbx_dom_weak/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use rbx_types::{Ref, Variant};
use serde::{Deserialize, Serialize};
use tsify::Tsify;

/**
Represents an instance that can be turned into a new
Expand Down Expand Up @@ -183,7 +185,7 @@ impl InstanceBuilder {
///
/// Operations that could affect other instances contained in the
/// [`WeakDom`][crate::WeakDom] cannot be performed on an `Instance` correctly.
#[derive(Debug)]
#[derive(Tsify, Debug, Serialize, Deserialize)]
pub struct Instance {
pub(crate) referent: Ref,
pub(crate) children: Vec<Ref>,
Expand Down
3 changes: 3 additions & 0 deletions rbx_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ rand = "0.8.5"
thiserror = "1.0.31"
serde = { version = "1.0.137", features = ["derive"], optional = true }

tsify = "0.4.5"
wasm-bindgen = "0.2.84"

[dev-dependencies]
insta = { version = "1.14.1", features = ["yaml"] }
bincode = "1.3.3"
Expand Down
4 changes: 3 additions & 1 deletion rbx_types/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ use std::{
iter::FromIterator,
};

use tsify::Tsify;

use crate::{Error, Variant};

use self::reader::read_attributes;
use self::writer::write_attributes;

pub(crate) use self::error::AttributeError;

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Tsify, Debug, Default, Clone, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
5 changes: 4 additions & 1 deletion rbx_types/src/axes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fmt;

use tsify::Tsify;

use crate::lister::Lister;

bitflags::bitflags! {
#[derive(Tsify)]
struct AxisFlags: u8 {
const X = 1;
const Y = 2;
Expand All @@ -14,7 +17,7 @@ bitflags::bitflags! {
///
/// ## See Also
/// * [Axes on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Axes)
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Clone, Copy, PartialEq, Eq)]
pub struct Axes {
flags: AxisFlags,
}
Expand Down
41 changes: 21 additions & 20 deletions rbx_types/src/basic_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use thiserror::Error;
use tsify::Tsify;

use crate::Error;

Expand All @@ -9,7 +10,7 @@ use crate::Error;
///
/// A list of all enums and their values are available [on the Roblox Developer
/// Hub](https://developer.roblox.com/en-us/api-reference/enum).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -34,7 +35,7 @@ impl Enum {
/// ## See Also
/// * [`Vector2int16`][struct.Vector2int16.html]
/// * [Vector2 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Vector2)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Vector2 {
pub x: f32,
pub y: f32,
Expand All @@ -54,7 +55,7 @@ impl Vector2 {
/// * [Vector2int16 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Vector2int16)
///
/// [Vector2]: struct.Vector2.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vector2int16 {
pub x: i16,
pub y: i16,
Expand All @@ -71,7 +72,7 @@ impl Vector2int16 {
/// ## See Also
/// * [`Vector3int16`][struct.Vector3int16.html]
/// * [Vector3 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Vector3)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Vector3 {
pub x: f32,
pub y: f32,
Expand Down Expand Up @@ -135,7 +136,7 @@ impl Vector3 {
/// * [Vector3int16 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Vector3int16)
///
/// [Vector3]: struct.Vector3.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vector3int16 {
pub x: i16,
pub y: i16,
Expand All @@ -152,7 +153,7 @@ impl Vector3int16 {
///
/// ## See Also
/// * [CFrame on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/CFrame)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -174,7 +175,7 @@ impl CFrame {

/// Used to represent the `orientation` field of `CFrame` and not a standalone
/// type in Roblox.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Matrix3 {
pub x: Vector3,
pub y: Vector3,
Expand Down Expand Up @@ -360,7 +361,7 @@ impl Matrix3 {
/// * [`Color3uint8`](struct.Color3uint8.html), which is used instead of
/// `Color3` on some types and does not represent HDR colors.
/// * [Color3 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Color3)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Color3 {
pub r: f32,
pub g: f32,
Expand Down Expand Up @@ -393,7 +394,7 @@ impl From<Color3uint8> for Color3 {
/// colors.
///
/// [BasePart.Color]: https://developer.roblox.com/en-us/api-reference/property/BasePart/Color
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Color3uint8 {
pub r: u8,
pub g: u8,
Expand Down Expand Up @@ -424,7 +425,7 @@ impl From<Color3> for Color3uint8 {
/// * [Ray on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Ray)
///
/// [FindPartOnRay]: https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRay
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -446,7 +447,7 @@ impl Ray {
/// ## See Also
/// * [`Region3int16`](struct.Region3int16.html)
/// * [Region3 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Region3)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Region3 {
pub min: Vector3,
pub max: Vector3,
Expand All @@ -466,7 +467,7 @@ impl Region3 {
/// * [Region3int16 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Region3int16)
///
/// [Region3]: struct.Region3.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Region3int16 {
pub min: Vector3int16,
pub max: Vector3int16,
Expand All @@ -482,7 +483,7 @@ impl Region3int16 {
///
/// ## See Also
/// * [Rect on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/Rect)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct Rect {
pub min: Vector2,
pub max: Vector2,
Expand All @@ -499,7 +500,7 @@ impl Rect {
///
/// ## See Also
/// * [UDim on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/UDim)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct UDim {
pub scale: f32,
pub offset: i32,
Expand All @@ -516,7 +517,7 @@ impl UDim {
///
/// ## See Also
/// * [UDim2 on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/UDim2)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct UDim2 {
pub x: UDim,
pub y: UDim,
Expand All @@ -532,7 +533,7 @@ impl UDim2 {
///
/// ## See Also
/// * [NumberRange on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/NumberRange)
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
pub struct NumberRange {
pub min: f32,
pub max: f32,
Expand All @@ -548,7 +549,7 @@ impl NumberRange {
///
/// ## See Also
/// * [ColorSequence on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/ColorSequence)
#[derive(Debug, Clone, PartialEq)]
#[derive(Tsify, Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -564,7 +565,7 @@ pub struct ColorSequence {
/// * [ColorSequenceKeypoint on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/ColorSequenceKeypoint)
///
/// [ColorSequence]: struct.ColorSequence.html
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -586,7 +587,7 @@ impl ColorSequenceKeypoint {
///
/// ## See Also
/// * [NumberSequence on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/NumberSequence)
#[derive(Debug, Clone, PartialEq)]
#[derive(Tsify, Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -603,7 +604,7 @@ pub struct NumberSequence {
/// * [NumberSequenceKeypoint on Roblox Developer Hub](https://developer.roblox.com/en-us/api-reference/datatype/NumberSequenceKeypoint)
///
/// [NumberSequence]: struct.NumberSequence.html
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Tsify, Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
4 changes: 3 additions & 1 deletion rbx_types/src/binary_string.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use tsify::Tsify;

/// Container for untyped binary data.
///
/// `BinaryString` is used in cases where the type of the underlying data is
/// unknown or unimplemented. Where possible, stronger types that interpret the
/// underlying bytes should be preferred.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
#[derive(Tsify, Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct BinaryString {
buffer: Vec<u8>,
}
Expand Down
Loading