Skip to content

Commit dce1b68

Browse files
author
Guy Bedford
authored
builtins: TextEncoder, TextDecoder, URL shared builtins (#438)
1 parent e637b8e commit dce1b68

18 files changed

+54
-42
lines changed

c-dependencies/js-compute-runtime/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
/js-compute-runtime*.wasm
66
/build
7+
/shared

c-dependencies/js-compute-runtime/Makefile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ $(OBJ_DIR)/core:
154154
$(OBJ_DIR)/host_interface:
155155
$(call cmd,mkdir,$@)
156156

157+
shared:
158+
$(call cmd,mkdir,$@)
159+
157160
# Downloaded dependencies ######################################################
158161

159162
$(BUILD)/openssl-$(OPENSSL_VERSION).tar.gz: URL=https://www.openssl.org/source/openssl-$(OPENSSL_VERSION).tar.gz
@@ -283,12 +286,15 @@ $(OBJ_DIR)/js-compute-runtime-component.wasm: $(OBJ_DIR)/xqd-world/xqd_world_ada
283286
$(WASI_CXX) $(LD_FLAGS) $(OPENSSL_LIBS) -o $@ $^
284287
$(call cmd_format,WASM_STRIP,$@) $(call WASM_STRIP,$@)
285288

286-
# The subset of javascript builtins that are reusable across other runtimes.
287-
shared-builtins.a: $(OBJ_DIR)/shared-builtins.a
288-
$(call cmd,cp,$@)
289+
# Shared builtins build
289290

290-
$(OBJ_DIR)/shared-builtins.a: $(OBJ_DIR)/builtins/shared/console.o $(OBJ_DIR)/builtin.o
291-
$(call cmd,wasi_ar,$@)
291+
shared-builtins: shared/builtins.a shared/librust_url.a
292+
293+
shared/builtins.a: $(OBJ_DIR)/builtins/shared/*.o $(OBJ_DIR)/builtin.o | shared
294+
$(call cmd,wasi_ar,$^)
295+
296+
shared/librust_url.a: $(RUST_URL_LIB) | shared
297+
$(call cmd,cp,$@)
292298

293299
# These two rules copy the built artifacts into the $(FSM_SRC) directory, and
294300
# are both marked phony as we need to do the right thing when running the

c-dependencies/js-compute-runtime/builtin-error-numbers.msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ MSG_DEF(JSMSG_BUILTIN_CTOR_NO_NEW, 1, JSEXN_TYPEERR,
4444
MSG_DEF(JSMSG_ILLEGAL_CTOR, 0, JSEXN_TYPEERR, "Illegal constructor")
4545
MSG_DEF(JSMSG_INVALID_INTERFACE, 2, JSEXN_TYPEERR, "'{0}' called on an object that does not implement interface {1}")
4646
MSG_DEF(JSMSG_INCOMPATIBLE_INSTANCE, 2, JSEXN_TYPEERR, "Method {0} called on receiver that's not an instance of {1}")
47+
MSG_DEF(JSMSG_INVALID_BUFFER_ARG, 2, JSEXN_TYPEERR, "{0} must be of type ArrayBuffer or ArrayBufferView but got \"{1}\"")
4748
//clang-format on

c-dependencies/js-compute-runtime/builtin.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ const JSErrorFormatString *GetErrorMessageBuiltin(void *userRef, unsigned errorN
88
return nullptr;
99
}
1010

11+
std::optional<std::span<uint8_t>> value_to_buffer(JSContext *cx, JS::HandleValue val,
12+
const char *val_desc) {
13+
if (!val.isObject() ||
14+
!(JS_IsArrayBufferViewObject(&val.toObject()) || JS::IsArrayBufferObject(&val.toObject()))) {
15+
JS_ReportErrorNumberUTF8(cx, GetErrorMessageBuiltin, nullptr, JSMSG_INVALID_BUFFER_ARG,
16+
val_desc, val.type());
17+
return std::nullopt;
18+
}
19+
20+
JS::RootedObject input(cx, &val.toObject());
21+
uint8_t *data;
22+
bool is_shared;
23+
size_t len = 0;
24+
25+
if (JS_IsArrayBufferViewObject(input)) {
26+
js::GetArrayBufferViewLengthAndData(input, &len, &is_shared, &data);
27+
} else {
28+
JS::GetArrayBufferLengthAndData(input, &len, &is_shared, &data);
29+
}
30+
31+
return std::span(data, len);
32+
}
33+
1134
JS::UniqueChars encode(JSContext *cx, JS::HandleString str, size_t *encoded_len) {
1235
JS::UniqueChars text = JS_EncodeStringToUTF8(cx, str);
1336
if (!text)

c-dependencies/js-compute-runtime/builtin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77
#pragma clang diagnostic push
88
#pragma clang diagnostic ignored "-Winvalid-offsetof"
99
#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"
10+
#include "js/ArrayBuffer.h"
1011
#include "js/Conversions.h"
1112
#include "js/ForOfIterator.h"
1213
#include "js/Object.h"
1314
#include "js/Promise.h"
15+
#include "js/experimental/TypedData.h"
1416
#include "jsapi.h"
1517
#include "jsfriendapi.h"
1618
#include "rust-url/rust-url.h"
19+
#include <span>
1720

1821
#pragma clang diagnostic pop
1922

23+
std::optional<std::span<uint8_t>> value_to_buffer(JSContext *cx, JS::HandleValue val,
24+
const char *val_desc);
25+
2026
// TODO(performance): introduce a version that writes into an existing buffer, and use that
2127
// with the hostcall buffer where possible.
2228
// https://github.com/fastly/js-compute-runtime/issues/215

c-dependencies/js-compute-runtime/builtins/fastly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "builtins/env.h"
1313
#include "builtins/fastly.h"
1414
#include "builtins/logger.h"
15-
#include "builtins/url.h"
15+
#include "builtins/shared/url.h"
1616
#include "core/geo_ip.h"
1717
#include "host_interface/host_call.h"
1818

c-dependencies/js-compute-runtime/builtins/fetch-event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "builtins/client-info.h"
33
#include "builtins/fastly.h"
44
#include "builtins/request-response.h"
5-
#include "builtins/url.h"
5+
#include "builtins/shared/url.h"
66
#include "builtins/worker-location.h"
77
#include "host_interface/host_api.h"
88
#include "xqd-world/xqd_world_adapter.h"

c-dependencies/js-compute-runtime/builtins/object-store.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "builtin.h"
1818
#include "builtins/native-stream-source.h"
1919
#include "builtins/object-store.h"
20-
#include "builtins/url.h"
20+
#include "builtins/shared/url.h"
2121
#include "host_interface/host_api.h"
2222
#include "host_interface/host_call.h"
2323
#include "js-compute-builtins.h"

c-dependencies/js-compute-runtime/builtins/request-response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "builtins/fetch-event.h"
77
#include "builtins/native-stream-source.h"
88
#include "builtins/object-store.h"
9+
#include "builtins/shared/url.h"
910
#include "builtins/transform-stream.h"
10-
#include "builtins/url.h"
1111
#include "host_interface/host_api.h"
1212
#include "third_party/picosha2.h"
1313

c-dependencies/js-compute-runtime/builtins/text-decoder.cpp renamed to c-dependencies/js-compute-runtime/builtins/shared/text-decoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "builtins/text-decoder.h"
2-
#include "js-compute-builtins.h"
1+
#include "builtins/shared/text-decoder.h"
2+
#include "builtin.h"
33

44
namespace builtins {
55

0 commit comments

Comments
 (0)