Skip to content

Commit 2a94208

Browse files
authored
Merge pull request tree-sitter#822 from tree-sitter/wasm-fixes
Update to the latest Emscripten and fix some WASM issues
2 parents 53949b0 + d3f30e2 commit 2a94208

File tree

9 files changed

+58
-48
lines changed

9 files changed

+58
-48
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ matrix:
1414

1515
before_install:
1616
# Install node
17-
- nvm install 10
18-
- nvm use 10
17+
- nvm install 12
18+
- nvm use 12
1919

2020
# Download emscripten and create a shorthand for adding it to the PATH.
2121
# Don't add it to the path globally because it overrides the default
@@ -58,8 +58,6 @@ deploy:
5858
file_glob: true
5959
file:
6060
- "tree-sitter-*.gz"
61-
- "target/release/tree-sitter.js"
62-
- "target/release/tree-sitter.wasm"
6361
draft: true
6462
overwrite: true
6563
skip_cleanup: true

cli/src/wasm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu
5757
}
5858

5959
// Run `emcc` in a container using the `emscripten-slim` image
60-
command.args(&["trzeci/emscripten-slim", "emcc"]);
60+
command.args(&["emscripten/emsdk", "emcc"]);
6161
} else {
6262
return Error::err(
6363
"You must have either emcc or docker on your PATH to run this command".to_string(),

lib/binding_web/binding.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,10 @@ extern void tree_sitter_parse_callback(
115115
);
116116

117117
extern void tree_sitter_log_callback(
118-
void *payload,
119-
TSLogType log_type,
118+
bool is_lex_message,
120119
const char *message
121120
);
122121

123-
void ts_parser_new_wasm() {
124-
TSParser *parser = ts_parser_new();
125-
char *input_buffer = calloc(INPUT_BUFFER_SIZE, sizeof(char));
126-
TRANSFER_BUFFER[0] = parser;
127-
TRANSFER_BUFFER[1] = input_buffer;
128-
}
129-
130122
static const char *call_parse_callback(
131123
void *payload,
132124
uint32_t byte,
@@ -148,8 +140,23 @@ static const char *call_parse_callback(
148140
return buffer;
149141
}
150142

143+
static void call_log_callback(
144+
void *payload,
145+
TSLogType log_type,
146+
const char *message
147+
) {
148+
tree_sitter_log_callback(log_type == TSLogTypeLex, message);
149+
}
150+
151+
void ts_parser_new_wasm() {
152+
TSParser *parser = ts_parser_new();
153+
char *input_buffer = calloc(INPUT_BUFFER_SIZE, sizeof(char));
154+
TRANSFER_BUFFER[0] = parser;
155+
TRANSFER_BUFFER[1] = input_buffer;
156+
}
157+
151158
void ts_parser_enable_logger_wasm(TSParser *self, bool should_log) {
152-
TSLogger logger = {self, should_log ? tree_sitter_log_callback : NULL};
159+
TSLogger logger = {self, should_log ? call_log_callback : NULL};
153160
ts_parser_set_logger(self, logger);
154161
}
155162

lib/binding_web/binding.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const SIZE_OF_RANGE = 2 * SIZE_OF_INT + 2 * SIZE_OF_POINT;
77
const ZERO_POINT = {row: 0, column: 0};
88
const QUERY_WORD_REGEX = /[\w-.]*/g;
99

10-
const PREDICATE_STEP_TYPE_DONE = 0;
1110
const PREDICATE_STEP_TYPE_CAPTURE = 1;
1211
const PREDICATE_STEP_TYPE_STRING = 2;
1312

@@ -880,7 +879,7 @@ class Language {
880879
}
881880

882881
return bytes
883-
.then(bytes => loadWebAssemblyModule(bytes, {loadAsync: true}))
882+
.then(bytes => loadSideModule(bytes, {loadAsync: true}))
884883
.then(mod => {
885884
const symbolNames = Object.keys(mod)
886885
const functionName = symbolNames.find(key =>
@@ -1140,7 +1139,3 @@ function marshalEdit(edit) {
11401139
}
11411140

11421141
Parser.Language = Language;
1143-
1144-
return Parser;
1145-
1146-
}));

lib/binding_web/imports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mergeInto(LibraryManager.library, {
1616
}
1717
},
1818

19-
tree_sitter_log_callback: function(_payload, isLexMessage, messageAddress) {
19+
tree_sitter_log_callback: function(isLexMessage, messageAddress) {
2020
if (currentLogCallback) {
2121
const message = UTF8ToString(messageAddress);
2222
currentLogCallback(message, isLexMessage !== 0);

lib/binding_web/suffix.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
return Parser;
2+
}));

script/build-wasm

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ EOF
3030
set -e
3131

3232
web_dir=lib/binding_web
33-
exports=$(cat ${web_dir}/exports.json)
3433
emscripten_flags="-O3"
3534
minify_js=1
3635
force_docker=0
@@ -64,11 +63,11 @@ emcc=
6463
if which emcc > /dev/null && [[ "$force_docker" == "0" ]]; then
6564
emcc=emcc
6665
elif which docker > /dev/null; then
67-
emcc="docker run \
68-
--rm \
69-
-v $(pwd):/src:Z \
70-
-u $(id -u) \
71-
trzeci/emscripten-slim \
66+
emcc="docker run \
67+
--rm \
68+
-v $(pwd):/src:Z \
69+
-u $(id -u) \
70+
emscripten/emsdk \
7271
emcc"
7372
else
7473
echo 'You must have either `docker` or `emcc` on your PATH to run this script'
@@ -79,24 +78,27 @@ mkdir -p target/scratch
7978

8079
# Use emscripten to generate `tree-sitter.js` and `tree-sitter.wasm`
8180
# in the `target/scratch` directory
82-
$emcc \
83-
-s WASM=1 \
84-
-s TOTAL_MEMORY=33554432 \
85-
-s ALLOW_MEMORY_GROWTH=1 \
86-
-s MAIN_MODULE=2 \
87-
-s NO_FILESYSTEM=1 \
88-
-s "EXPORTED_FUNCTIONS=${exports}" \
89-
$emscripten_flags \
90-
-std=c99 \
91-
-D 'fprintf(...)=' \
92-
-D NDEBUG= \
93-
-I lib/src \
94-
-I lib/include \
95-
--js-library ${web_dir}/imports.js \
96-
--pre-js ${web_dir}/prefix.js \
97-
--post-js ${web_dir}/binding.js \
98-
lib/src/lib.c \
99-
${web_dir}/binding.c \
81+
$emcc \
82+
-s WASM=1 \
83+
-s TOTAL_MEMORY=33554432 \
84+
-s ALLOW_MEMORY_GROWTH=1 \
85+
-s MAIN_MODULE=2 \
86+
-s NO_FILESYSTEM=1 \
87+
-s NODEJS_CATCH_EXIT=0 \
88+
-s NODEJS_CATCH_REJECTION=0 \
89+
-s EXPORTED_FUNCTIONS=@${web_dir}/exports.json \
90+
$emscripten_flags \
91+
-std=c99 \
92+
-D 'fprintf(...)=' \
93+
-D NDEBUG= \
94+
-I lib/src \
95+
-I lib/include \
96+
--js-library ${web_dir}/imports.js \
97+
--pre-js ${web_dir}/prefix.js \
98+
--post-js ${web_dir}/binding.js \
99+
--post-js ${web_dir}/suffix.js \
100+
lib/src/lib.c \
101+
${web_dir}/binding.c \
100102
-o target/scratch/tree-sitter.js
101103

102104
# Use terser to write a minified version of `tree-sitter.js` into

script/fetch-emscripten

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
EMSCRIPTEN_VERSION=1.39.15
5+
EMSCRIPTEN_VERSION=2.0.9
66

77
mkdir -p target
88
EMSDK_DIR="./target/emsdk"

script/generate-fixtures-wasm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ set -e
44

55
cargo build --release
66

7+
build_wasm_args=
8+
if [[ $1 == "--docker" ]]; then
9+
build_wasm_args="--docker"
10+
shift
11+
fi
12+
713
filter_grammar_name=$1
814

915
root_dir=$PWD
@@ -20,7 +26,7 @@ while read -r grammar_file; do
2026
fi
2127

2228
echo "Compiling ${grammar_name} parser to wasm"
23-
"$tree_sitter" build-wasm $grammar_dir
29+
"$tree_sitter" build-wasm $build_wasm_args $grammar_dir
2430
done <<< "$grammar_files"
2531

2632
mv tree-sitter-*.wasm target/release/

0 commit comments

Comments
 (0)