forked from ada-url/ada
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: emscripten support (ada-url#412)
* emscripten support * Trying to fix the new CI test * Trying to add test * Only preduce wasm.js when building for wasm * Automating the tests. * Fix. * Adding tests Co-authored-by: Moshe Atlow <[email protected]> * Reformat * Fixing ci. * Removing dead code. --------- Co-authored-by: Moshe Atlow <[email protected]>
- Loading branch information
Showing
7 changed files
with
149 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: emscripten | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened, ready_for_review] | ||
paths-ignore: | ||
- '**.md' | ||
- 'docs/**' | ||
push: | ||
branches: | ||
- main | ||
paths-ignore: | ||
- '**.md' | ||
- 'docs/**' | ||
|
||
permissions: | ||
contents: read | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-node@v3 | ||
- uses: mymindstorm/setup-emsdk@v12 | ||
- name: Verify | ||
run: emcc -v | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Configure | ||
run: emcmake cmake -B buildwasm | ||
- name: Build | ||
run: cmake --build buildwasm | ||
- name: Test | ||
run: ctest --test-dir buildwasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
link_libraries(ada) | ||
add_executable(wasm wasm.cpp) | ||
set_target_properties(wasm PROPERTIES LINK_FLAGS "-Os -s WASM=1 -s ENVIRONMENT=node -s EXPORT_NAME=loadWASM -s MODULARIZE=1 --bind --no-entry") | ||
configure_file(test.js.in test.js) | ||
|
||
find_program(NODEJS_BINARY NAMES node nodejs) | ||
if(NODEJS_BINARY) | ||
add_test(NAME wasmtest | ||
COMMAND "${NODEJS_BINARY}" "${CMAKE_CURRENT_BINARY_DIR}/test.js") | ||
endif(NODEJS_BINARY) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const child_process = require('child_process'); | ||
const assert = require('assert'); | ||
const test = require('node:test'); | ||
const wasm = require('${CMAKE_CURRENT_BINARY_DIR}/wasm'); | ||
|
||
function toJS(obj) { | ||
const result = {}; | ||
for (const key of Object.keys(obj.__proto__)) { | ||
result[key] = typeof obj[key] === "object" ? toJS(obj[key]) : obj[key]; | ||
} | ||
return result; | ||
} | ||
|
||
const expected = { | ||
"result": "success", | ||
"href": "https://google.com/?q=Yagiz#Nizipli", | ||
"type": 2, | ||
"components": { | ||
"protocol_end": 6, | ||
"username_end": 8, | ||
"host_start": 8, | ||
"host_end": 18, | ||
"port": 4294967295, | ||
"pathname_start": 18, | ||
"search_start": 19, | ||
"hash_start": 27 | ||
} | ||
}; | ||
|
||
test('wasm', async () => { | ||
const { parse } = await wasm(); | ||
console.log(); | ||
assert.deepStrictEqual(toJS(parse('https://google.com/?q=Yagiz#Nizipli')), expected); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "ada.h" | ||
#include <emscripten/emscripten.h> | ||
#include <emscripten/bind.h> | ||
|
||
using namespace emscripten; | ||
|
||
struct parse_result { | ||
std::string result; | ||
std::string href; | ||
uint32_t type; | ||
ada::url_components components; | ||
}; | ||
|
||
parse_result parse(const std::string &input) { | ||
auto out = ada::parse<ada::url_aggregator>(input); | ||
parse_result result; | ||
if (!out.has_value()) { | ||
result.result = "fail"; | ||
} else { | ||
result.result = "success"; | ||
result.href = std::string(out->get_href()); | ||
result.type = out->type; | ||
result.components = out->get_components(); | ||
} | ||
return result; | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(url_components) { | ||
class_<parse_result>("Result") | ||
.property("result", &parse_result::result) | ||
.property("href", &parse_result::href) | ||
.property("type", &parse_result::type) | ||
.property("components", &parse_result::components); | ||
class_<ada::url_components>("URLComponents") | ||
.property("protocol_end", &ada::url_components::protocol_end) | ||
.property("username_end", &ada::url_components::username_end) | ||
.property("host_start", &ada::url_components::host_start) | ||
.property("host_end", &ada::url_components::host_end) | ||
.property("port", &ada::url_components::port) | ||
.property("pathname_start", &ada::url_components::pathname_start) | ||
.property("search_start", &ada::url_components::search_start) | ||
.property("hash_start", &ada::url_components::hash_start); | ||
|
||
function("parse", &parse); | ||
} |