Skip to content

Commit 7148b68

Browse files
Martin SplittMartin Splitt
authored andcommitted
Adds WASM build
1 parent 50a0dac commit 7148b68

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
2+
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
23

34
package(default_visibility = ["//visibility:public"])
45

@@ -60,3 +61,24 @@ cc_binary(
6061
":robots",
6162
],
6263
)
64+
65+
cc_binary(
66+
name = "robots_js",
67+
srcs = ["robots_wasm.cc"],
68+
deps = [
69+
":robots",
70+
],
71+
copts = [
72+
"--bind"
73+
],
74+
linkopts = [
75+
"-l", "embind",
76+
"-s", "EXPORTED_FUNCTIONS=_IsAllowed",
77+
"-s", "EXPORTED_RUNTIME_METHODS=ccall,cwrap"
78+
],
79+
)
80+
81+
wasm_cc_binary(
82+
name = "robots_wasm",
83+
cc_target = ":robots_js",
84+
)

MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ bazel_dep(
2323
name = "rules_cc",
2424
version = "0.2.17",
2525
)
26+
27+
bazel_dep(
28+
name = "emsdk",
29+
version = "5.0.4"
30+
)

robots_wasm.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <emscripten/bind.h>
2+
#include "robots.h"
3+
4+
// Returns true if the given user agent is allowed to crawl the URL given the robots content
5+
// Otherwise, returns false.
6+
// We also need to make sure our function gets exported as IsAllowed
7+
bool IsAllowed(std::string user_agent, std::string url, std::string robots_content) asm("IsAllowed");
8+
bool IsAllowed(std::string user_agent, std::string url, std::string robots_content) {
9+
googlebot::RobotsMatcher matcher;
10+
std::vector<std::string> user_agents(1, user_agent);
11+
return matcher.AllowedByRobots(robots_content, &user_agents, url);
12+
}
13+
14+
// Create a binding so we can call the function from JavaScript
15+
// Example: Module.IsAllowed('googlebot', 'https://example.com/test', 'user-agent: *\ndisallow: /') // will return false
16+
EMSCRIPTEN_BINDINGS(my_module) {
17+
emscripten::function("IsAllowed", &IsAllowed);
18+
}
19+

0 commit comments

Comments
 (0)