Add haxelib layer: extern + build-macro JSON override for the Lua target#5
Add haxelib layer: extern + build-macro JSON override for the Lua target#5jdonaldson wants to merge 1 commit into
Conversation
This makes hx-lua-simdjson installable as a haxelib alongside the existing luarock, so haxe.Json can opt into simdjson on the Lua target without simdjson being a std dependency. - src/hxluasimdjson/Json.hx: extern binding to the native hxsimdjson module - src/hxluasimdjson/Macro.hx: init macro that attaches a @:build to haxe.format.JsonParser and rewrites parse() to call the simdjson extern, guarded to the Lua target (no-op elsewhere, including the macro/eval context) - extraParams.hxml: runs the init macro automatically when -lib'd - haxelib.json: haxelib metadata The build-macro approach avoids shadowing std/haxe/format/JsonParser.hx, so it does not need re-syncing when the Haxe std changes across versions.
|
This approach looks good to me, the build macro is probably the cleanest way to do this without duplicating classes. Just some thoughts about less crucial decisions:
|
|
Thanks for the review! On the two points: No-op vs. error on non-Lua — I looked at how existing target-specific haxelibs handle this. That said, this lib rewrites (Aside: Same vs. separate git history — I think same history is fine and doesn't really add to the upstream-merge conflict surface. The Haxe additions live in separate paths from the vendored sources: If you'd like the split to be more explicit without going to separate branches, I can move the Haxe classpath into its own top-level dir (e.g. |
|
Another organic comment here, hope you're having a great summer! |
This makes
hx-lua-simdjsoninstallable as a haxelib alongside the existing luarock, sohaxe.Jsoncan opt into simdjson on the Lua target without simdjson being a std dependency. Groundwork for HaxeFoundation/haxe#12771 (making the simdjson parser opt-in rather than the Lua default).What is added
src/hxluasimdjson/Json.hxhxsimdjsonmodule (@:luaRequire)src/hxluasimdjson/Macro.hx@:buildtohaxe.format.JsonParserand rewritesparse()to call the simdjson externextraParams.hxml-libhaxelib.jsonhaxelib submitted)Usage:
Why a build macro instead of shadowing the std file
haxe.format.JsonParsercould be overridden by shipping a classpath copy ofstd/haxe/format/JsonParser.hx, but that copies the entire std file and has to be re-synced every time the std parser changes across Haxe versions (raised in HaxeFoundation/haxe#12771). Instead, the macro attaches@:buildto the existing std type viaCompiler.addGlobalMetadataand rewrites only theparsebody -- nothing is shadowed, nothing needs syncing.Scope / no-op safety
The
@:buildis attached globally, sopatch()also runs whenhaxe.format.JsonParseris built for the macro/eval context (where the Lua-only extern does not exist). The rewrite is therefore guarded withContext.defined("lua")and is a no-op on every other target and in the macro context. Onlyparseis affected;haxe.Json.stringifykeeps using the std printer.Testing
Verified against a local Haxe build with the std
JsonParsertemporarily flipped to the #12771 default (pure-Haxe parser):hxsimdjsonreference in output)-lib hx-lua-simdjson->haxe.Json.parsecompiles torequire("hxsimdjson").parse(...)--interpand non-Lua targets -> clean no-op (interp parses correctly via the pure-Haxe fallback)Runtime parsing through the native module was not exercised here because the
hxsimdjsonrock is not built in this environment; the change only affects which parse function the Lua codegen emits.