Skip to content
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
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
[submodule "third_party/wabt"]
path = third_party/wabt
url = https://github.com/WebAssembly/wabt.git
[submodule "third_party/testsuite"]
path = third_party/testsuite
url = https://github.com/MotleyCoderDev/testsuite.git
[submodule "third_party/binaryen"]
path = third_party/binaryen
url = https://github.com/MotleyCoderDev/binaryen.git
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"rootDir": "${workspaceFolder}/project",
"outDir": "${workspaceFolder}/build/vscode_roku_deploy",
"enableDebuggerAutoRecovery": false,
"stopDebuggerOnAppExit": false
"stopDebuggerOnAppExit": false,
"injectRdbOnDeviceComponent": true
}
]
}
20 changes: 15 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
cmake_minimum_required(VERSION 3.13.4)
project(WASM2BRS VERSION 1.0.0)

add_subdirectory(third_party/wabt)
add_subdirectory(third_party/binaryen)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include_directories(${WABT_SOURCE_DIR} ${WABT_BINARY_DIR})
if(NOT DEFINED ENV{WABT_INCLUDE_DIR})
message(FATAL_ERROR "WABT_INCLUDE_DIR environment variable is not defined!")
endif()
set(WABT_LIB_DIR $ENV{WABT_LIB_DIR})

if(NOT DEFINED ENV{WABT_LIB_DIR})
message(FATAL_ERROR "WABT_LIB_DIR environment variable is not defined!")
endif()
set(WABT_INCLUDE_DIR $ENV{WABT_INCLUDE_DIR})

include_directories(${WABT_INCLUDE_DIR})

add_executable(wasm2brs src/wasm2brs.cc src/brs-writer.cc)

add_dependencies(wasm2brs wabt)
target_link_libraries(wasm2brs wabt)
target_link_libraries(wasm2brs PRIVATE ${WABT_LIB_DIR}/libwabt.a)
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ doom: build/doom/doom-wasm.out.brs
cp samples/doom/manifest project/manifest

build/doom/doom-wasm.out.brs: build/doom/doom.wasm build/wasm2brs/wasm2brs
./build/wasm2brs/third_party/binaryen/bin/wasm-opt -g -O4 ./build/doom/doom.wasm -o ./build/doom/doom-opt.wasm
wasm-opt -g -O4 ./build/doom/doom.wasm -o ./build/doom/doom-opt.wasm
./build/wasm2brs/wasm2brs -o build/doom/doom-wasm.out.brs ./build/doom/doom-opt.wasm

build/doom/doom.wasm: build/doom/Makefile FORCE
GNUMAKEFLAGS=--no-print-directory cmake --build ./build/doom --parallel

build/doom/Makefile:
mkdir -p build/doom
cd build/doom && wasimake cmake ../../samples/doom
cd build/doom && cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake ../../samples/doom

# --- files
files: build/files/files-wasm.out.brs
Expand All @@ -76,12 +76,12 @@ files: build/files/files-wasm.out.brs
cp samples/files/manifest project/manifest

build/files/files-wasm.out.brs: build/files/files.wasm build/wasm2brs/wasm2brs
./build/wasm2brs/third_party/binaryen/bin/wasm-opt -g -Oz ./build/files/files.wasm -o ./build/files/files-opt.wasm
wasm-opt -O4 ./build/files/files.wasm -o ./build/files/files-opt.wasm
./build/wasm2brs/wasm2brs -o build/files/files-wasm.out.brs ./build/files/files-opt.wasm

build/files/files.wasm: samples/files/files.cc
mkdir -p build/files
wasic++ -g -Oz samples/files/files.cc -o ./build/files/files.wasm
wasm32-wasi-clang -g -Oz samples/files/files.cc -o ./build/files/files.wasm

# --- cmake
cmake: build/cmake/cmake-wasm.out.brs
Expand All @@ -95,7 +95,7 @@ build/cmake/cmake-wasm.out.brs: build/cmake/Makefile build/wasm2brs/wasm2brs FOR

build/cmake/Makefile:
mkdir -p build/cmake
cd build/cmake && wasimake cmake ../../samples/cmake
cd build/cmake && cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake ../../samples/cmake

# --- mandelbrot
mandelbrot: build/mandelbrot/mandelbrot-wasm.out.brs
Expand All @@ -105,12 +105,12 @@ mandelbrot: build/mandelbrot/mandelbrot-wasm.out.brs
cp samples/mandelbrot/manifest project/manifest

build/mandelbrot/mandelbrot-wasm.out.brs: build/mandelbrot/mandelbrot.wasm build/wasm2brs/wasm2brs
./build/wasm2brs/third_party/binaryen/bin/wasm-opt -O4 ./build/mandelbrot/mandelbrot.wasm -o ./build/mandelbrot/mandelbrot-opt.wasm
wasm-opt -O4 ./build/mandelbrot/mandelbrot.wasm -o ./build/mandelbrot/mandelbrot-opt.wasm
./build/wasm2brs/wasm2brs -o build/mandelbrot/mandelbrot-wasm.out.brs ./build/mandelbrot/mandelbrot-opt.wasm

build/mandelbrot/mandelbrot.wasm: samples/mandelbrot/mandelbrot.c
mkdir -p build/mandelbrot
clang -Ofast --target=wasm32 -nostdlib -Wl,--no-entry samples/mandelbrot/mandelbrot.c -o ./build/mandelbrot/mandelbrot.wasm
wasm32-wasi-clang -O3 -ffast-math --target=wasm32 -nostdlib -Wl,--no-entry samples/mandelbrot/mandelbrot.c -o ./build/mandelbrot/mandelbrot.wasm

# --- javascript
javascript: build/javascript/javascript-wasm.out.brs
Expand All @@ -120,15 +120,15 @@ javascript: build/javascript/javascript-wasm.out.brs
cp samples/javascript/manifest project/manifest

build/javascript/javascript-wasm.out.brs: build/javascript/javascript.wasm build/wasm2brs/wasm2brs
./build/wasm2brs/third_party/binaryen/bin/wasm-opt -g -O4 ./build/javascript/javascript.wasm -o ./build/javascript/javascript-opt.wasm
./build/wasm2brs/wasm2brs -o build/javascript/javascript-wasm.out.brs ./build/javascript/javascript-opt.wasm
wasm-opt -g -O4 ./build/javascript/javascript.wasm -o ./build/javascript/javascript-opt.wasm
./build/wasm2brs/wasm2brs --enable-exceptions -o build/javascript/javascript-wasm.out.brs ./build/javascript/javascript-opt.wasm

build/javascript/javascript.wasm: build/javascript/Makefile FORCE
GNUMAKEFLAGS=--no-print-directory cmake --build ./build/javascript --parallel

build/javascript/Makefile:
mkdir -p build/javascript
cd build/javascript && wasimake cmake ../../samples/javascript
cd build/javascript && emcmake cmake ../../samples/javascript

# --- rust
rust: build/rust/rust-wasm.out.brs
Expand All @@ -138,7 +138,7 @@ rust: build/rust/rust-wasm.out.brs
cp samples/rust/manifest project/manifest

build/rust/rust-wasm.out.brs: build/rust/rust.wasm build/wasm2brs/wasm2brs
./build/wasm2brs/third_party/binaryen/bin/wasm-opt -g -O4 ./build/rust/rust.wasm -o ./build/rust/rust-opt.wasm
wasm-opt -g -O4 ./build/rust/rust.wasm -o ./build/rust/rust-opt.wasm
./build/wasm2brs/wasm2brs -o build/rust/rust-wasm.out.brs ./build/rust/rust-opt.wasm

build/rust/rust.wasm: samples/rust/rust.rs
Expand Down
57 changes: 45 additions & 12 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2020, Trevor Sundberg. See LICENSE.md
FROM ubuntu:20.04
FROM ubuntu:24.04

ARG DEBIAN_FRONTEND=noninteractive

Expand All @@ -16,22 +16,55 @@ RUN apt-get update && \
python3-pip \
dumb-init

RUN ln /usr/bin/lld /usr/bin/wasm-ld
# Install libssl1.1 manually.
# Needed for running wasm2c
RUN curl -LO http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb \
&& apt-get install -y ./libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb \
&& rm libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb

RUN groupadd -g 1000 group && useradd -g 1000 -u 1000 -ms /bin/bash user
USER user
WORKDIR /home/user
# Emscripten SDK
RUN git clone https://github.com/emscripten-core/emsdk.git /opt/emsdk && \
cd /opt/emsdk && \
./emsdk install 4.0.11 && \
./emsdk activate 4.0.11
ENV EMSDK="/opt/emsdk"
ENV PATH="/opt/emsdk:/opt/emsdk/upstream/emscripten:${PATH}"

RUN curl https://raw.githubusercontent.com/wasienv/wasienv/master/install.sh | sh
# Wabt (WebAssembly Binary Toolkit)
RUN curl -LO https://github.com/WebAssembly/wabt/releases/download/1.0.37/wabt-1.0.37-ubuntu-20.04.tar.gz && \
tar -xzf wabt-1.0.37-ubuntu-20.04.tar.gz && \
mv wabt-1.0.37 /opt/wabt && \
rm wabt-1.0.37-ubuntu-20.04.tar.gz
ENV PATH="/opt/wabt/bin:${PATH}"
ENV WABT_INCLUDE_DIR="/opt/wabt/include"
ENV WABT_LIB_DIR="/opt/wabt/lib"

ENV WASMER_DIR="/home/user/.wasmer"
ENV WASMER_CACHE_DIR="/home/user/.wasmer/cache"
ENV PATH="/home/user/.wasmer/bin:/home/user/.wasienv/bin/:${PATH}:/home/user/.wasmer/globals/wapm_packages/.bin"
# Binaryen
RUN curl -LO https://github.com/WebAssembly/binaryen/releases/download/version_123/binaryen-version_123-x86_64-linux.tar.gz && \
tar -xzf binaryen-version_123-x86_64-linux.tar.gz && \
mv binaryen-version_123 /opt/binaryen && \
rm binaryen-version_123-x86_64-linux.tar.gz
ENV PATH="/opt/binaryen/bin:${PATH}"

RUN curl -sSf https://sh.rustup.rs | bash -s -- -y
ENV PATH="/home/user/.cargo/bin:${PATH}"
# Wasi SDK
RUN curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-27/wasi-sdk-27.0-x86_64-linux.deb && \
dpkg -i wasi-sdk-27.0-x86_64-linux.deb && \
rm wasi-sdk-27.0-x86_64-linux.deb
ENV PATH="/opt/wasi-sdk/bin:${PATH}"
ENV WASI_SDK_PATH="/opt/wasi-sdk"

# Wasmer
ENV WASMER_DIR=/opt/wasmer
RUN curl https://get.wasmer.io -sSfL | sh
ENV PATH="${WASMER_DIR}/bin:${PATH}"

# Rust
ENV RUSTUP_HOME=/opt/rust
ENV CARGO_HOME=/opt/rust
ENV PATH="/opt/rust/bin:${PATH}"
RUN curl -sSf https://sh.rustup.rs | bash -s -- -y --default-toolchain=1.81.0
RUN rustup target add wasm32-wasi
RUN cargo install cargo-wasi
RUN cargo install --locked cargo-wasi

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD echo "No command specified"
4 changes: 0 additions & 4 deletions include/roku.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
#ifndef ROKU_H
#define ROKU_H

#ifdef __wasilibc_unmodified_upstream
#define ROKU_BUILTIN(name) __attribute__((__import_name__(#name)))
#else
#define ROKU_BUILTIN(name)
#endif

/*
This function causes the script to pause for the specified time, without wasting CPU cycles.
Expand Down
6 changes: 6 additions & 0 deletions project/source/Main.brs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Function CatchingStart()
Start()
Catch e
Print e
For i = 0 to e.backtrace.Count() -1
backtrace = e.backtrace[i]
Print backtrace.function;" ";backtrace.filename.replace("pkg:/", "./project/");":";backtrace.line_number.ToStr()
End For
End Try
EndFunction

Expand All @@ -55,6 +59,8 @@ sub Main()
scene = sgScreen.CreateScene("main")
sgScreen.show()

' vscode_rdb_on_device_component_entry

m.keyboard = scene.findNode("keyboard")
m.keyboard.setFocus(True)

Expand Down
22 changes: 14 additions & 8 deletions project/source/runtime.brs
Original file line number Diff line number Diff line change
Expand Up @@ -625,17 +625,17 @@ Function F64Log(x as Double) as Double

xStr = x.ToStr()

max = 709.782712893#
min = -744.440071921#
maxValue = 709.782712893#
minMin = -744.440071921#

While True
guess = (max + min) / 2#
guess = (maxValue + minMin) / 2#
value = 2.71828182845904523536# ^ guess
If F64Abs(value - x) < 1e-5# Or value.ToStr() = xStr Return guess
If value > x Then
max = guess
maxValue = guess
Else
min = guess
minMin = guess
End If
End While
End Function
Expand Down Expand Up @@ -1007,9 +1007,15 @@ Function MemoryGrow(memory as Object, maxPages as Integer, deltaPages as Integer
End Function

Function MemoryCopy(dstBytes as Object, dst as Integer, srcBytes as Object, src as Integer, size as Integer) as Integer
For i = 0 To size - 1
dstBytes[dst + i] = srcBytes[src + i]
End For
If dst < src Or dst >= src + size Then
For i = 0 To size - 1
dstBytes[dst + i] = srcBytes[src + i]
End For
Else
For i = size - 1 To 0 Step -1
dstBytes[dst + i] = srcBytes[src + i]
End For
End If
Return size
End Function

Expand Down
5 changes: 3 additions & 2 deletions samples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(cmake)
set(WASM2BRS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../.." CACHE STRING "The wasm2brs repo directory")

# Enable many optimizations which helps reduce code size
add_compile_options(-Ofast)
add_compile_options(-O3 -ffast-math)

# The ignored warnings are due to the preamble.h in wasienv
add_compile_options(-Wno-macro-redefined -Wno-writable-strings -Wno-return-type)
Expand All @@ -13,12 +13,13 @@ add_compile_options(-Wno-macro-redefined -Wno-writable-strings -Wno-return-type)
add_link_options(-Wl,--allow-undefined)

add_executable(cmake main.cc)
set_target_properties(cmake PROPERTIES OUTPUT_NAME cmake.wasm)

target_include_directories(cmake PRIVATE "${WASM2BRS_DIR}/include")

# Perform wasm specific optimizations (required to reduce BrightScript variables and goto/labels)
add_custom_command(TARGET cmake POST_BUILD
COMMAND "${WASM2BRS_DIR}/build/wasm2brs/third_party/binaryen/bin/wasm-opt" -g -Oz cmake.wasm -o cmake-opt.wasm
COMMAND "wasm-opt" -g -Oz cmake.wasm -o cmake-opt.wasm
)

# Output the final optimized wasm file to BrightScript
Expand Down
6 changes: 3 additions & 3 deletions samples/cmake/cmake.brs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Function Start()
' The Init function is generated by wasm2brs and sets up the
' memory object as well as the imports, exports, data-sections, etc.
w2bInit__()
_cmake_wasmInit__()

' WASI is the WebAssembly System Interface, or how WebAssembly talks
' to the operating system, or in this case Roku. Here you can pass
' environment variables, command line arguments, etc. The m.w2b_memory
' is the name of the roByteArray created by w2bInit__.
wasi_init(m.w2b_memory, "cmake.wasm", {})
wasi_init(m._cmake_wasm_memory, "cmake.wasm", {})

' Most compilers such as clang generate a wrapper around main() called
' _start(), which is responsible for all pre-main initialialization, and
' it eventually calls main itself.
w2b__start()
_cmake_wasm__start()

' Shutdown WASI, which in our case flushes the output, etc.
wasi_shutdown()
Expand Down
1 change: 1 addition & 0 deletions samples/doom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ src/r_sky.c
src/st_lib.c
src/wi_stuff.c
)
set_target_properties(doom PROPERTIES OUTPUT_NAME doom.wasm)

target_compile_options(doom PRIVATE "-Wno-everything")
target_include_directories(doom PRIVATE ../../include)
2 changes: 1 addition & 1 deletion samples/doom/src/p_doors.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ EV_VerticalDoor
case 32:
case 33:
case 34:
door->type = open;
door->type = opendoor;
line->special = 0;
break;

Expand Down
2 changes: 1 addition & 1 deletion samples/doom/src/p_enemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ void A_KeenDie (mobj_t* mo)
}

junk.tag = 666;
EV_DoDoor(&junk,open);
EV_DoDoor(&junk,opendoor);
}


Expand Down
10 changes: 5 additions & 5 deletions samples/doom/src/p_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,13 @@ P_CrossSpecialLine
// All from here to RETRIGGERS.
case 2:
// Open Door
EV_DoDoor(line,open);
EV_DoDoor(line,opendoor);
line->special = 0;
break;

case 3:
// Close Door
EV_DoDoor(line,close);
EV_DoDoor(line,closedoor);
line->special = 0;
break;

Expand Down Expand Up @@ -801,7 +801,7 @@ P_CrossSpecialLine

case 75:
// Close Door
EV_DoDoor(line,close);
EV_DoDoor(line,closedoor);
break;

case 76:
Expand Down Expand Up @@ -846,7 +846,7 @@ P_CrossSpecialLine

case 86:
// Open Door
EV_DoDoor(line,open);
EV_DoDoor(line,opendoor);
break;

case 87:
Expand Down Expand Up @@ -987,7 +987,7 @@ P_ShootSpecialLine

case 46:
// OPEN DOOR
EV_DoDoor(line,open);
EV_DoDoor(line,opendoor);
P_ChangeSwitchTexture(line,1);
break;

Expand Down
Loading