-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquickjs-arraybuffer-sink.c
More file actions
211 lines (161 loc) · 5.09 KB
/
quickjs-arraybuffer-sink.c
File metadata and controls
211 lines (161 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "defines.h"
#include <cutils.h>
#include <libregexp.h>
#include "property-enumeration.h"
#include <quickjs.h>
#include <string.h>
#include "debug.h"
#include "buffer-utils.h"
/**
* \defgroup quickjs-tree-walker quickjs-arraybuffer_sink: Object tree walker
* @{
*/
VISIBLE JSClassID js_arraybuffer_sink_class_id = 0;
static JSValue arraybuffer_sink_proto, arraybuffer_sink_ctor;
static JSValue
js_arraybuffer_sink_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
DynBuf* s;
JSValue proto, obj = JS_UNDEFINED;
int argi = 1;
if(!(s = js_mallocz(ctx, sizeof(DynBuf))))
return JS_EXCEPTION;
dbuf_init2(s, 0, 0);
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_arraybuffer_sink_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, s);
return obj;
fail:
js_free(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
enum {
METHOD_WRITE,
METHOD_FLUSH,
METHOD_END,
};
static void
js_arraybuffer_sink_free(JSRuntime* rt, void* opaque, void* ptr) {
js_free_rt(rt, ptr);
}
static JSValue
js_arraybuffer_sink_method(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
DynBuf* s;
JSValue ret = JS_UNDEFINED;
if(!(s = JS_GetOpaque2(ctx, this_val, js_arraybuffer_sink_class_id)))
return JS_EXCEPTION;
switch(magic) {
case METHOD_WRITE: {
if(!s->realloc_func) {
return JS_ThrowInternalError(ctx, "ArrayBufferSink has ended");
}
InputBuffer buf = js_input_args(ctx, argc, argv);
if(buf.data && buf.size) {
if(dbuf_put(s, inputbuffer_data(&buf), inputbuffer_length(&buf))) {
inputbuffer_free(&buf, ctx);
return JS_ThrowInternalError(ctx, "Unable to write to ArrayBufferSink");
}
ret = JS_NewInt32(ctx, inputbuffer_length(&buf));
}
inputbuffer_free(&buf, ctx);
break;
}
case METHOD_FLUSH: {
if(s->buf && s->size) {
ret = JS_NewArrayBuffer(ctx, s->buf, s->size, js_arraybuffer_sink_free, 0, FALSE);
dbuf_init2(s, 0, 0);
}
break;
}
case METHOD_END: {
if(s->buf && s->size) {
ret = JS_NewArrayBuffer(ctx, s->buf, s->size, js_arraybuffer_sink_free, 0, FALSE);
s->buf = 0;
dbuf_free(s);
}
break;
}
}
return ret;
}
enum {
PROP_SIZE,
};
static JSValue
js_arraybuffer_sink_get(JSContext* ctx, JSValueConst this_val, int magic) {
DynBuf* s;
JSValue ret = JS_UNDEFINED;
if(!(s = JS_GetOpaque2(ctx, this_val, js_arraybuffer_sink_class_id)))
return JS_EXCEPTION;
switch(magic) {
case PROP_SIZE: {
ret = JS_NewUint32(ctx, s->size);
break;
}
}
return ret;
}
static JSValue
js_arraybuffer_sink_set(JSContext* ctx, JSValueConst this_val, JSValueConst value, int magic) {
DynBuf* s;
JSValue ret = JS_UNDEFINED;
if(!(s = JS_GetOpaque2(ctx, this_val, js_arraybuffer_sink_class_id)))
return JS_EXCEPTION;
switch(magic) {}
return ret;
}
static void
js_arraybuffer_sink_finalizer(JSRuntime* rt, JSValue val) {
DynBuf* s;
if((s = JS_GetOpaque(val, js_arraybuffer_sink_class_id))) {
dbuf_free(s);
js_free_rt(rt, s);
}
}
static JSClassDef js_arraybuffer_sink_class = {
.class_name = "ArrayBufferSink",
.finalizer = js_arraybuffer_sink_finalizer,
};
static const JSCFunctionListEntry js_arraybuffer_sink_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("write", 1, js_arraybuffer_sink_method, METHOD_WRITE),
JS_CFUNC_MAGIC_DEF("flush", 0, js_arraybuffer_sink_method, METHOD_FLUSH),
JS_CFUNC_MAGIC_DEF("end", 0, js_arraybuffer_sink_method, METHOD_END),
JS_CGETSET_MAGIC_DEF("size", js_arraybuffer_sink_get, 0, PROP_SIZE),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "ArrayBufferSink", JS_PROP_CONFIGURABLE),
};
static int
js_arraybuffer_sink_init(JSContext* ctx, JSModuleDef* m) {
JS_NewClassID(&js_arraybuffer_sink_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_arraybuffer_sink_class_id, &js_arraybuffer_sink_class);
arraybuffer_sink_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, arraybuffer_sink_proto, js_arraybuffer_sink_proto_funcs, countof(js_arraybuffer_sink_proto_funcs));
JS_SetClassProto(ctx, js_arraybuffer_sink_class_id, arraybuffer_sink_proto);
arraybuffer_sink_ctor = JS_NewCFunction2(ctx, js_arraybuffer_sink_constructor, "ArrayBufferSink", 1, JS_CFUNC_constructor, 0);
JS_SetConstructor(ctx, arraybuffer_sink_ctor, arraybuffer_sink_proto);
if(m) {
JS_SetModuleExport(ctx, m, "ArrayBufferSink", arraybuffer_sink_ctor);
}
return 0;
}
#ifdef JS_SHARED_LIBRARY
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_arraybuffer_sink
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_arraybuffer_sink_init))) {
JS_AddModuleExport(ctx, m, "ArrayBufferSink");
}
return m;
}
/**
* @}
*/