Skip to content

Commit 7609a03

Browse files
Jake ChampionJakeChampion
authored andcommitted
extract dictionary namespace into its own files
1 parent 1e6dce4 commit 7609a03

File tree

3 files changed

+96
-68
lines changed

3 files changed

+96
-68
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "dictionary.h"
2+
#include "host_call.h"
3+
4+
namespace builtins {
5+
6+
DictionaryHandle Dictionary::dictionary_handle(JSObject *obj) {
7+
JS::Value val = JS::GetReservedSlot(obj, Dictionary::Slots::Handle);
8+
return DictionaryHandle{static_cast<uint32_t>(val.toInt32())};
9+
}
10+
11+
bool Dictionary::get(JSContext *cx, unsigned argc, JS::Value *vp) {
12+
METHOD_HEADER(1)
13+
14+
size_t name_len;
15+
JS::UniqueChars name = encode(cx, args[0], &name_len);
16+
17+
OwnedHostCallBuffer buffer;
18+
size_t nwritten = 0;
19+
int status = xqd_dictionary_get(Dictionary::dictionary_handle(self), name.get(), name_len,
20+
buffer.get(), DICTIONARY_ENTRY_MAX_LEN, &nwritten);
21+
// Status code 10 indicates the key wasn't found, so we return null.
22+
if (status == 10) {
23+
args.rval().setNull();
24+
return true;
25+
}
26+
27+
// Ensure that we throw an exception for all unexpected host errors.
28+
if (!HANDLE_RESULT(cx, status))
29+
return false;
30+
31+
JS::RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(buffer.get(), nwritten)));
32+
if (!text)
33+
return false;
34+
35+
args.rval().setString(text);
36+
return true;
37+
}
38+
39+
const JSFunctionSpec Dictionary::methods[] = {JS_FN("get", get, 1, JSPROP_ENUMERATE), JS_FS_END};
40+
41+
const JSPropertySpec Dictionary::properties[] = {JS_PS_END};
42+
43+
bool Dictionary::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
44+
REQUEST_HANDLER_ONLY("The Dictionary builtin");
45+
CTOR_HEADER("Dictionary", 1);
46+
47+
size_t name_len;
48+
JS::UniqueChars name = encode(cx, args[0], &name_len);
49+
JS::RootedObject dictionary(cx, JS_NewObjectForConstructor(cx, &class_, args));
50+
DictionaryHandle dict_handle = {INVALID_HANDLE};
51+
if (!HANDLE_RESULT(cx, xqd_dictionary_open(name.get(), name_len, &dict_handle)))
52+
return false;
53+
54+
JS::SetReservedSlot(dictionary, Dictionary::Slots::Handle,
55+
JS::Int32Value((int)dict_handle.handle));
56+
if (!dictionary)
57+
return false;
58+
args.rval().setObject(*dictionary);
59+
return true;
60+
}
61+
62+
bool Dictionary::init_class(JSContext *cx, JS::HandleObject global) {
63+
return BuiltinImpl<Dictionary>::init_class_impl(cx, global);
64+
}
65+
66+
} // namespace builtins
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef JS_COMPUTE_RUNTIME_DICTIONARY_H
2+
#define JS_COMPUTE_RUNTIME_DICTIONARY_H
3+
4+
#include "builtin.h"
5+
6+
namespace builtins {
7+
8+
class Dictionary : public BuiltinImpl<Dictionary> {
9+
private:
10+
public:
11+
static constexpr const char *class_name = "Dictionary";
12+
static const int ctor_length = 1;
13+
enum Slots { Handle, Count };
14+
15+
static const JSFunctionSpec methods[];
16+
static const JSPropertySpec properties[];
17+
18+
static bool get(JSContext *cx, unsigned argc, JS::Value *vp);
19+
20+
static DictionaryHandle dictionary_handle(JSObject *obj);
21+
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
22+
23+
static bool init_class(JSContext *cx, JS::HandleObject global);
24+
};
25+
26+
} // namespace builtins
27+
28+
#endif

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

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "builtins/compression-stream.h"
3636
#include "builtins/console.h"
3737
#include "builtins/decompression-stream.h"
38+
#include "builtins/dictionary.h"
3839
#include "builtins/env.h"
3940
#include "builtins/fastly.h"
4041
#include "builtins/logger.h"
@@ -2491,73 +2492,6 @@ JSObject *create(JSContext *cx, HandleObject response, ResponseHandle response_h
24912492
}
24922493
} // namespace Response
24932494

2494-
namespace Dictionary {
2495-
namespace Slots {
2496-
enum { Dictionary, Count };
2497-
};
2498-
2499-
DictionaryHandle dictionary_handle(JSObject *obj) {
2500-
JS::Value val = JS::GetReservedSlot(obj, Slots::Dictionary);
2501-
return DictionaryHandle{static_cast<uint32_t>(val.toInt32())};
2502-
}
2503-
2504-
const unsigned ctor_length = 1;
2505-
2506-
bool check_receiver(JSContext *cx, HandleValue receiver, const char *method_name);
2507-
2508-
bool get(JSContext *cx, unsigned argc, Value *vp) {
2509-
METHOD_HEADER(1)
2510-
2511-
size_t name_len;
2512-
UniqueChars name = encode(cx, args[0], &name_len);
2513-
2514-
OwnedHostCallBuffer buffer;
2515-
size_t nwritten = 0;
2516-
int status = xqd_dictionary_get(dictionary_handle(self), name.get(), name_len, buffer.get(),
2517-
DICTIONARY_ENTRY_MAX_LEN, &nwritten);
2518-
// Status code 10 indicates the key wasn't found, so we return null.
2519-
if (status == 10) {
2520-
args.rval().setNull();
2521-
return true;
2522-
}
2523-
2524-
// Ensure that we throw an exception for all unexpected host errors.
2525-
if (!HANDLE_RESULT(cx, status))
2526-
return false;
2527-
2528-
RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(buffer.get(), nwritten)));
2529-
if (!text)
2530-
return false;
2531-
2532-
args.rval().setString(text);
2533-
return true;
2534-
}
2535-
2536-
const JSFunctionSpec methods[] = {JS_FN("get", get, 1, JSPROP_ENUMERATE), JS_FS_END};
2537-
2538-
const JSPropertySpec properties[] = {JS_PS_END};
2539-
bool constructor(JSContext *cx, unsigned argc, Value *vp);
2540-
CLASS_BOILERPLATE(Dictionary)
2541-
2542-
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
2543-
REQUEST_HANDLER_ONLY("The Dictionary builtin");
2544-
CTOR_HEADER("Dictionary", 1);
2545-
2546-
size_t name_len;
2547-
UniqueChars name = encode(cx, args[0], &name_len);
2548-
RootedObject dictionary(cx, JS_NewObjectForConstructor(cx, &class_, args));
2549-
DictionaryHandle dict_handle = {INVALID_HANDLE};
2550-
if (!HANDLE_RESULT(cx, xqd_dictionary_open(name.get(), name_len, &dict_handle)))
2551-
return false;
2552-
2553-
JS::SetReservedSlot(dictionary, Slots::Dictionary, JS::Int32Value((int)dict_handle.handle));
2554-
if (!dictionary)
2555-
return false;
2556-
args.rval().setObject(*dictionary);
2557-
return true;
2558-
}
2559-
} // namespace Dictionary
2560-
25612495
namespace TextEncoder {
25622496
namespace Slots {
25632497
enum { Count };
@@ -5032,7 +4966,7 @@ bool define_fastly_sys(JSContext *cx, HandleObject global) {
50324966
return false;
50334967
if (!Response::init_class(cx, global))
50344968
return false;
5035-
if (!Dictionary::init_class(cx, global))
4969+
if (!builtins::Dictionary::init_class(cx, global))
50364970
return false;
50374971
if (!Headers::init_class(cx, global))
50384972
return false;

0 commit comments

Comments
 (0)