Skip to content

Commit 23665ab

Browse files
Jake ChampionJakeChampion
authored andcommitted
Allow CompressionStream to be used as a base class to extend from within application javascript
1 parent 0b06bf9 commit 23665ab

File tree

1 file changed

+59
-61
lines changed

1 file changed

+59
-61
lines changed

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

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,45 +3366,8 @@ uint8_t *output_buffer(JSObject *self) {
33663366
}
33673367

33683368
const unsigned ctor_length = 1;
3369-
JSObject *create(JSContext *cx, Format format);
33703369
bool check_receiver(JSContext *cx, HandleValue receiver, const char *method_name);
33713370

3372-
/**
3373-
* https://wicg.github.io/compression/#dom-compressionstream-compressionstream
3374-
*/
3375-
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
3376-
// 1. If _format_ is unsupported in `CompressionStream`, then throw a
3377-
// `TypeError`.
3378-
CTOR_HEADER("CompressionStream", 1);
3379-
3380-
size_t format_len;
3381-
UniqueChars format_chars = encode(cx, args[0], &format_len);
3382-
if (!format_chars)
3383-
return false;
3384-
3385-
Format format;
3386-
if (!strcmp(format_chars.get(), "deflate")) {
3387-
format = Format::Deflate;
3388-
} else if (!strcmp(format_chars.get(), "gzip")) {
3389-
format = Format::GZIP;
3390-
} else {
3391-
JS_ReportErrorUTF8(cx,
3392-
"'format' has to be \"deflate\" or \"gzip\", "
3393-
"but got \"%s\"",
3394-
format_chars.get());
3395-
return false;
3396-
}
3397-
3398-
// Steps 2-6.
3399-
RootedObject stream(cx, create(cx, format));
3400-
if (!stream) {
3401-
return false;
3402-
}
3403-
3404-
args.rval().setObject(*stream);
3405-
return true;
3406-
}
3407-
34083371
// Steps 1-5 of the transform algorithm, and 1-4 of the flush algorithm.
34093372
bool deflate_chunk(JSContext *cx, HandleObject self, HandleValue chunk, bool finished) {
34103373
z_stream *zstream = state(self);
@@ -3549,36 +3512,15 @@ const JSFunctionSpec methods[] = {JS_FS_END};
35493512
const JSPropertySpec properties[] = {JS_PSG("readable", readable_get, JSPROP_ENUMERATE),
35503513
JS_PSG("writable", writable_get, JSPROP_ENUMERATE), JS_PS_END};
35513514

3515+
bool constructor(JSContext *cx, unsigned argc, Value *vp);
3516+
35523517
CLASS_BOILERPLATE_CUSTOM_INIT(CompressionStream)
35533518

35543519
static PersistentRooted<JSObject *> transformAlgo;
35553520
static PersistentRooted<JSObject *> flushAlgo;
35563521

3557-
bool init_class(JSContext *cx, HandleObject global) {
3558-
if (!init_class_impl(cx, global)) {
3559-
return false;
3560-
}
3561-
3562-
JSFunction *transformFun = JS_NewFunction(cx, transformAlgorithm, 1, 0, "CS Transform");
3563-
if (!transformFun)
3564-
return false;
3565-
transformAlgo.init(cx, JS_GetFunctionObject(transformFun));
3566-
3567-
JSFunction *flushFun = JS_NewFunction(cx, flushAlgorithm, 1, 0, "CS Flush");
3568-
if (!flushFun)
3569-
return false;
3570-
flushAlgo.init(cx, JS_GetFunctionObject(flushFun));
3571-
3572-
return true;
3573-
}
3574-
35753522
// Steps 2-6 of `new CompressionStream()`.
3576-
JSObject *create(JSContext *cx, Format format) {
3577-
RootedObject stream(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
3578-
if (!stream) {
3579-
return nullptr;
3580-
}
3581-
3523+
JSObject *create(JSContext *cx, HandleObject stream, Format format) {
35823524
RootedValue stream_val(cx, ObjectValue(*stream));
35833525

35843526
// 2. Set this's format to _format_.
@@ -3640,6 +3582,62 @@ JSObject *create(JSContext *cx, Format format) {
36403582

36413583
return stream;
36423584
}
3585+
3586+
/**
3587+
* https://wicg.github.io/compression/#dom-compressionstream-compressionstream
3588+
*/
3589+
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
3590+
// 1. If _format_ is unsupported in `CompressionStream`, then throw a
3591+
// `TypeError`.
3592+
CTOR_HEADER("CompressionStream", 1);
3593+
3594+
size_t format_len;
3595+
UniqueChars format_chars = encode(cx, args[0], &format_len);
3596+
if (!format_chars)
3597+
return false;
3598+
3599+
Format format;
3600+
if (!strcmp(format_chars.get(), "deflate")) {
3601+
format = Format::Deflate;
3602+
} else if (!strcmp(format_chars.get(), "gzip")) {
3603+
format = Format::GZIP;
3604+
} else {
3605+
JS_ReportErrorUTF8(cx,
3606+
"'format' has to be \"deflate\" or \"gzip\", "
3607+
"but got \"%s\"",
3608+
format_chars.get());
3609+
return false;
3610+
}
3611+
3612+
RootedObject compressionStreamInstance(cx, JS_NewObjectForConstructor(cx, &class_, args));
3613+
// Steps 2-6.
3614+
RootedObject stream(cx, create(cx, compressionStreamInstance, format));
3615+
if (!stream) {
3616+
return false;
3617+
}
3618+
3619+
args.rval().setObject(*stream);
3620+
return true;
3621+
}
3622+
3623+
bool init_class(JSContext *cx, HandleObject global) {
3624+
if (!init_class_impl(cx, global)) {
3625+
return false;
3626+
}
3627+
3628+
JSFunction *transformFun = JS_NewFunction(cx, transformAlgorithm, 1, 0, "CS Transform");
3629+
if (!transformFun)
3630+
return false;
3631+
transformAlgo.init(cx, JS_GetFunctionObject(transformFun));
3632+
3633+
JSFunction *flushFun = JS_NewFunction(cx, flushAlgorithm, 1, 0, "CS Flush");
3634+
if (!flushFun)
3635+
return false;
3636+
flushAlgo.init(cx, JS_GetFunctionObject(flushFun));
3637+
3638+
return true;
3639+
}
3640+
36433641
} // namespace CompressionStream
36443642

36453643
namespace CacheOverride {

0 commit comments

Comments
 (0)