Skip to content

Commit de241aa

Browse files
Fix web emscripten bindings, bump to 0.1.26 (#456)
Running `cd web; npm run build` against the latest main yields these errors: ```log + emcc --bind -o src/xgrammar_binding.js src/xgrammar_binding.cc build/libxgrammar.a -O3 -s EXPORT_ES6=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s NO_DYNAMIC_EXECUTION=1 -s MODULARIZE=1 -s SINGLE_FILE=1 -s EXPORTED_RUNTIME_METHODS=FS -s ALLOW_MEMORY_GROWTH=1 -I../include -I../3rdparty/picojson -I../3rdparty/dlpack/include src/xgrammar_binding.cc:147:34: error: use of undeclared identifier '_JSONSchemaToEBNF' 147 | function("_JSONSchemaToEBNF", &_JSONSchemaToEBNF); | ^~~~~~~~~~~~~~~~~ src/xgrammar_binding.cc:169:8: error: no matching member function for call to 'function' 165 | class_<GrammarCompiler>("GrammarCompiler") | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 166 | .constructor<const TokenizerInfo&, int, bool>() | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 167 | .function("CompileJSONSchema", &GrammarCompiler::CompileJSONSchema) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 168 | .function("CompileBuiltinJSONGrammar", &GrammarCompiler::CompileBuiltinJSONGrammar) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 169 | .function("CompileGrammar", &GrammarCompiler::CompileGrammar) | ~^~~~~~~~ /home/logan/emsdk/upstream/emscripten/cache/sysroot/include/emscripten/bind.h:1577:44: note: candidate template ignored: couldn't infer template argument 'Callable' 1577 | EMSCRIPTEN_ALWAYS_INLINE const class_& function(const char* methodName, Callable callable, Policies...) const { | ^ src/xgrammar_binding.cc:182:56: error: no member named '_DebugAcceptString' in 'xgrammar::GrammarMatcher' 182 | .function("_DebugAcceptString", &GrammarMatcher::_DebugAcceptString); | ^~~~~~~~~~~~~~~~~~ 3 errors generated. ``` It seems these were changed in #123 (_JSONSchemaToEBNF relocated), #327 (_DebugAcceptString renamed), #428 (CompileGrammar gains overload) I've: 1. Asked Claude to update these bindings for me 2. Tested the web example app 3. Built `web-llm` using this version of `web-xgrammar` and tested chat completions in my own app (grammar-constrained generation is much faster thanks to #308!) <img width="964" height="477" alt="image" src="https://github.com/user-attachments/assets/8529375c-ab3c-454b-91fd-e18134584035" /> --------- Co-authored-by: Yixin Dong <[email protected]>
1 parent 8409677 commit de241aa

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

web/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mlc-ai/web-xgrammar",
3-
"version": "0.1.0",
3+
"version": "0.1.26",
44
"description": "",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

web/src/xgrammar_binding.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iostream>
1414
#include <memory>
1515

16+
#include "../../cpp/json_schema_converter.h"
1617
#include "../../cpp/testing.h"
1718

1819
// #include "../../cpp/support/logging.h"
@@ -144,7 +145,18 @@ EMSCRIPTEN_BINDINGS(xgrammar) {
144145
function("vecIntToView", &vecIntToView);
145146

146147
// Testing methods
147-
function("_JSONSchemaToEBNF", &_JSONSchemaToEBNF);
148+
function(
149+
"_JSONSchemaToEBNF",
150+
select_overload<std::string(
151+
const std::string&,
152+
bool,
153+
std::optional<int>,
154+
std::optional<std::pair<std::string, std::string>>,
155+
bool,
156+
std::optional<int>,
157+
JSONFormat
158+
)>(&JSONSchemaToEBNF)
159+
);
148160
function("DebugGetMaskedTokensFromBitmask", &Testing_DebugGetMaskedTokensFromBitmask);
149161

150162
class_<Grammar>("Grammar")
@@ -166,7 +178,10 @@ EMSCRIPTEN_BINDINGS(xgrammar) {
166178
.constructor<const TokenizerInfo&, int, bool>()
167179
.function("CompileJSONSchema", &GrammarCompiler::CompileJSONSchema)
168180
.function("CompileBuiltinJSONGrammar", &GrammarCompiler::CompileBuiltinJSONGrammar)
169-
.function("CompileGrammar", &GrammarCompiler::CompileGrammar)
181+
.function(
182+
"CompileGrammar",
183+
select_overload<CompiledGrammar(const Grammar&)>(&GrammarCompiler::CompileGrammar)
184+
)
170185
.function("ClearCache", &GrammarCompiler::ClearCache);
171186

172187
class_<GrammarMatcher>("GrammarMatcher")
@@ -179,5 +194,5 @@ EMSCRIPTEN_BINDINGS(xgrammar) {
179194
.function("Reset", &GrammarMatcher::Reset)
180195
.function("FindJumpForwardString", &GrammarMatcher::FindJumpForwardString)
181196
.function("Rollback", &GrammarMatcher::Rollback)
182-
.function("_DebugAcceptString", &GrammarMatcher::_DebugAcceptString);
197+
.function("AcceptString", &GrammarMatcher::AcceptString);
183198
}

0 commit comments

Comments
 (0)