diff --git a/include/ant.h b/include/ant.h index 5e4d8da..999a332 100644 --- a/include/ant.h +++ b/include/ant.h @@ -114,8 +114,8 @@ ant_value_t js_mksym_well_known(ant_t *, const char *desc); ant_value_t js_mkfun_meta(const ant_cfunc_meta_t *meta); ant_value_t js_mkfun_dyn(ant_cfunc_t fn); -ant_value_t js_heavy_mkfun(ant_t *js, ant_value_t (*fn)(ant_t *, ant_value_t *, int), ant_value_t data); -ant_value_t js_heavy_mkfun_native(ant_t *js, ant_value_t (*fn)(ant_t *, ant_value_t *, int), void *ptr, uint32_t tag); +ant_value_t js_heavy_mkfun(ant_t *js, ant_value_t (*fn)(ant_params_t), ant_value_t data); +ant_value_t js_heavy_mkfun_native(ant_t *js, ant_value_t (*fn)(ant_params_t), void *ptr, uint32_t tag); ant_value_t js_mkprop_fast(ant_t *js, ant_value_t obj, const char *key, size_t len, ant_value_t v); // TODO: deprecate diff --git a/src/streams/codec.c b/src/streams/codec.c index efe4974..754bd8c 100644 --- a/src/streams/codec.c +++ b/src/streams/codec.c @@ -86,9 +86,11 @@ static ant_value_t codec_flush_controller(ant_value_t *args, int nargs) { } static ant_value_t tes_transform(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); + tes_state_t *st = js_check_native_tag(js->current_func, TES_NATIVE_TAG) + ? (tes_state_t *)js_get_native_ptr(js->current_func) + : NULL; - tes_state_t *st = (tes_state_t *)js_get_native_ptr(wrapper); + if (!st) return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid TextEncoderStream"); ant_value_t ctrl_obj = codec_transform_controller(args, nargs); ant_value_t chunk = (nargs > 0) ? args[0] : js_mkundef(); @@ -171,8 +173,11 @@ static ant_value_t tes_transform(ant_t *js, ant_value_t *args, int nargs) { } static ant_value_t tes_flush(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); - tes_state_t *st = (tes_state_t *)js_get_native_ptr(wrapper); + tes_state_t *st = js_check_native_tag(js->current_func, TES_NATIVE_TAG) + ? (tes_state_t *)js_get_native_ptr(js->current_func) + : NULL; + + if (!st) return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid TextEncoderStream"); ant_value_t ctrl_obj = codec_flush_controller(args, nargs); if (st->pending_len > 0) { @@ -220,12 +225,9 @@ static ant_value_t js_tes_ctor(ant_t *js, ant_value_t *args, int nargs) { js_set_native(obj, st, TES_NATIVE_TAG); js_set_finalizer(obj, tes_state_finalize); - ant_value_t wrapper = js_mkobj(js); - js_set_native(wrapper, st, TES_NATIVE_TAG); - ant_value_t transformer = js_mkobj(js); - ant_value_t transform_fn = js_heavy_mkfun(js, tes_transform, wrapper); - ant_value_t flush_fn = js_heavy_mkfun(js, tes_flush, wrapper); + ant_value_t transform_fn = js_heavy_mkfun_native(js, tes_transform, st, TES_NATIVE_TAG); + ant_value_t flush_fn = js_heavy_mkfun_native(js, tes_flush, st, TES_NATIVE_TAG); js_set(js, transformer, "transform", transform_fn); js_set(js, transformer, "flush", flush_fn); @@ -240,14 +242,16 @@ static ant_value_t js_tes_ctor(ant_t *js, ant_value_t *args, int nargs) { if (is_err(ts_obj)) { free(st); return ts_obj; } js_set_slot(obj, SLOT_ENTRIES, ts_obj); - js_set_slot(wrapper, SLOT_ENTRIES, ts_obj); return obj; } static ant_value_t tds_transform(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); - td_state_t *st = (td_state_t *)js_get_native_ptr(wrapper); + td_state_t *st = js_check_native_tag(js->current_func, TDS_NATIVE_TAG) + ? (td_state_t *)js_get_native_ptr(js->current_func) + : NULL; + + if (!st) return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid TextDecoderStream"); ant_value_t ctrl_obj = codec_transform_controller(args, nargs); ant_value_t chunk = (nargs > 0) ? args[0] : js_mkundef(); @@ -272,8 +276,11 @@ static ant_value_t tds_transform(ant_t *js, ant_value_t *args, int nargs) { } static ant_value_t tds_flush(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); - td_state_t *st = (td_state_t *)js_get_native_ptr(wrapper); + td_state_t *st = js_check_native_tag(js->current_func, TDS_NATIVE_TAG) + ? (td_state_t *)js_get_native_ptr(js->current_func) + : NULL; + + if (!st) return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid TextDecoderStream"); ant_value_t ctrl_obj = codec_flush_controller(args, nargs); ant_value_t result = td_decode(js, st, NULL, 0, false); @@ -394,12 +401,10 @@ static ant_value_t js_tds_ctor(ant_t *js, ant_value_t *args, int nargs) { js_set_native(obj, st, TDS_NATIVE_TAG); js_set_finalizer(obj, tds_state_finalize); - ant_value_t wrapper = js_mkobj(js); - js_set_native(wrapper, st, TDS_NATIVE_TAG); - ant_value_t transformer = js_mkobj(js); - ant_value_t transform_fn = js_heavy_mkfun(js, tds_transform, wrapper); - ant_value_t flush_fn = js_heavy_mkfun(js, tds_flush, wrapper); + ant_value_t transform_fn = js_heavy_mkfun_native(js, tds_transform, st, TDS_NATIVE_TAG); + ant_value_t flush_fn = js_heavy_mkfun_native(js, tds_flush, st, TDS_NATIVE_TAG); + js_set(js, transformer, "transform", transform_fn); js_set(js, transformer, "flush", flush_fn); @@ -415,9 +420,7 @@ static ant_value_t js_tds_ctor(ant_t *js, ant_value_t *args, int nargs) { js->this_val = saved_this; if (is_err(ts_obj)) { free(st); return ts_obj; } - js_set_slot(obj, SLOT_ENTRIES, ts_obj); - js_set_slot(wrapper, SLOT_ENTRIES, ts_obj); return obj; } diff --git a/src/streams/compression.c b/src/streams/compression.c index 4e94e0f..b00f074 100644 --- a/src/streams/compression.c +++ b/src/streams/compression.c @@ -58,11 +58,6 @@ static ant_value_t get_ts(ant_value_t obj) { return js_get_slot(obj, SLOT_ENTRIES); } -static zformat_t get_wrapper_format(ant_value_t wrapper) { - ant_value_t fmt = js_get_slot(wrapper, SLOT_CTOR); - return (zformat_t)(int)js_getnum(fmt); -} - bool cs_is_stream(ant_value_t obj) { return is_object_type(obj) && (js_check_native_tag(obj, CS_Z_NATIVE_TAG) || js_check_native_tag(obj, CS_BROTLI_NATIVE_TAG)) @@ -132,9 +127,9 @@ static ant_value_t enqueue_buffer(ant_t *js, ant_value_t ctrl_obj, const uint8_t #define ZCHUNK_SIZE 16384 static ant_value_t cs_transform(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); + uint32_t tag = js_get_native_tag(js->current_func); + zstate_t *st = (zstate_t *)js_get_native_ptr(js->current_func); - zstate_t *st = (zstate_t *)js_get_native_ptr(wrapper); ant_value_t ctrl_obj = (nargs > 1) ? args[1] : js_mkundef(); ant_value_t chunk = (nargs > 0) ? args[0] : js_mkundef(); @@ -146,10 +141,13 @@ static ant_value_t cs_transform(ant_t *js, ant_value_t *args, int nargs) { if (!input || input_len == 0) return js_mkundef(); - if (get_wrapper_format(wrapper) == ZFMT_BROTLI) { - brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(wrapper); + if (tag == CS_BROTLI_NATIVE_TAG) { + brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(js->current_func); return brotli_stream_transform(js, brotli_st, ctrl_obj, input, input_len); } + + if (tag != CS_Z_NATIVE_TAG || !st) + return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid CompressionStream"); st->strm.next_in = (Bytef *)input; st->strm.avail_in = (uInt)input_len; @@ -172,14 +170,16 @@ static ant_value_t cs_transform(ant_t *js, ant_value_t *args, int nargs) { } static ant_value_t cs_flush(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); + uint32_t tag = js_get_native_tag(js->current_func); ant_value_t ctrl_obj = (nargs > 0) ? args[0] : js_mkundef(); - if (get_wrapper_format(wrapper) == ZFMT_BROTLI) { - brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(wrapper); + if (tag == CS_BROTLI_NATIVE_TAG) { + brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(js->current_func); return brotli_stream_flush(js, brotli_st, ctrl_obj); } - zstate_t *st = (zstate_t *)js_get_native_ptr(wrapper); + + zstate_t *st = (zstate_t *)js_get_native_ptr(js->current_func); + if (tag != CS_Z_NATIVE_TAG || !st) return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid CompressionStream"); st->strm.next_in = NULL; st->strm.avail_in = 0; @@ -240,16 +240,22 @@ static ant_value_t js_cs_ctor(ant_t *js, ant_value_t *args, int nargs) { ant_value_t obj = js_mkobj(js); ant_value_t proto = js_instance_proto_from_new_target(js, g_cs_proto); if (is_object_type(proto)) js_set_proto_init(obj, proto); + js_set_native(obj, fmt == ZFMT_BROTLI ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI ? CS_BROTLI_NATIVE_TAG : CS_Z_NATIVE_TAG); js_set_finalizer(obj, fmt == ZFMT_BROTLI ? brotli_state_finalize : zstate_finalize); - ant_value_t wrapper = js_mkobj(js); - js_set_native(wrapper, fmt == ZFMT_BROTLI ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI ? CS_BROTLI_NATIVE_TAG : CS_Z_NATIVE_TAG); - js_set_slot(wrapper, SLOT_CTOR, js_mknum((double)fmt)); - ant_value_t transformer = js_mkobj(js); - ant_value_t transform_fn = js_heavy_mkfun(js, cs_transform, wrapper); - ant_value_t flush_fn = js_heavy_mkfun(js, cs_flush, wrapper); + + ant_value_t transform_fn = js_heavy_mkfun_native(js, cs_transform, fmt == ZFMT_BROTLI + ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI + ? CS_BROTLI_NATIVE_TAG : CS_Z_NATIVE_TAG + ); + + ant_value_t flush_fn = js_heavy_mkfun_native(js, cs_flush, fmt == ZFMT_BROTLI + ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI + ? CS_BROTLI_NATIVE_TAG : CS_Z_NATIVE_TAG + ); + js_set(js, transformer, "transform", transform_fn); js_set(js, transformer, "flush", flush_fn); @@ -268,14 +274,13 @@ static ant_value_t js_cs_ctor(ant_t *js, ant_value_t *args, int nargs) { return ts_obj; } js_set_slot(obj, SLOT_ENTRIES, ts_obj); - js_set_slot(wrapper, SLOT_ENTRIES, ts_obj); return obj; } static ant_value_t ds_transform(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); - zstate_t *st = (zstate_t *)js_get_native_ptr(wrapper); + uint32_t tag = js_get_native_tag(js->current_func); + zstate_t *st = (zstate_t *)js_get_native_ptr(js->current_func); ant_value_t ctrl_obj = (nargs > 1) ? args[1] : js_mkundef(); ant_value_t chunk = (nargs > 0) ? args[0] : js_mkundef(); @@ -286,10 +291,13 @@ static ant_value_t ds_transform(ant_t *js, ant_value_t *args, int nargs) { return js_mkerr_typed(js, JS_ERR_TYPE, "The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); if (!input || input_len == 0) return js_mkundef(); - if (get_wrapper_format(wrapper) == ZFMT_BROTLI) { - brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(wrapper); + if (tag == DS_BROTLI_NATIVE_TAG) { + brotli_stream_state_t *brotli_st = (brotli_stream_state_t *)js_get_native_ptr(js->current_func); return brotli_stream_transform(js, brotli_st, ctrl_obj, input, input_len); } + + if (tag != DS_Z_NATIVE_TAG || !st) + return js_mkerr_typed(js, JS_ERR_TYPE, "Invalid DecompressionStream"); st->strm.next_in = (Bytef *)input; st->strm.avail_in = (uInt)input_len; @@ -313,10 +321,10 @@ static ant_value_t ds_transform(ant_t *js, ant_value_t *args, int nargs) { } static ant_value_t ds_flush(ant_t *js, ant_value_t *args, int nargs) { - ant_value_t wrapper = js_get_slot(js->current_func, SLOT_DATA); + uint32_t tag = js_get_native_tag(js->current_func); ant_value_t ctrl_obj = (nargs > 0) ? args[0] : js_mkundef(); - if (get_wrapper_format(wrapper) == ZFMT_BROTLI) { - brotli_stream_state_t *st = (brotli_stream_state_t *)js_get_native_ptr(wrapper); + if (tag == DS_BROTLI_NATIVE_TAG) { + brotli_stream_state_t *st = (brotli_stream_state_t *)js_get_native_ptr(js->current_func); return brotli_stream_flush(js, st, ctrl_obj); } return js_mkundef(); @@ -361,16 +369,21 @@ static ant_value_t js_ds_ctor(ant_t *js, ant_value_t *args, int nargs) { ant_value_t obj = js_mkobj(js); ant_value_t proto = js_instance_proto_from_new_target(js, g_ds_proto); if (is_object_type(proto)) js_set_proto_init(obj, proto); + js_set_native(obj, fmt == ZFMT_BROTLI ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI ? DS_BROTLI_NATIVE_TAG : DS_Z_NATIVE_TAG); js_set_finalizer(obj, fmt == ZFMT_BROTLI ? brotli_state_finalize : zstate_inflate_finalize); - ant_value_t wrapper = js_mkobj(js); - js_set_native(wrapper, fmt == ZFMT_BROTLI ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI ? DS_BROTLI_NATIVE_TAG : DS_Z_NATIVE_TAG); - js_set_slot(wrapper, SLOT_CTOR, js_mknum((double)fmt)); - ant_value_t transformer = js_mkobj(js); - ant_value_t transform_fn = js_heavy_mkfun(js, ds_transform, wrapper); - ant_value_t flush_fn = js_heavy_mkfun(js, ds_flush, wrapper); + + ant_value_t transform_fn = js_heavy_mkfun_native(js, ds_transform, fmt == ZFMT_BROTLI + ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI + ? DS_BROTLI_NATIVE_TAG : DS_Z_NATIVE_TAG + ); + + ant_value_t flush_fn = js_heavy_mkfun_native(js, ds_flush, fmt == ZFMT_BROTLI + ? (void *)brotli : (void *)st, fmt == ZFMT_BROTLI + ? DS_BROTLI_NATIVE_TAG : DS_Z_NATIVE_TAG + ); js_set(js, transformer, "transform", transform_fn); js_set(js, transformer, "flush", flush_fn); @@ -391,8 +404,6 @@ static ant_value_t js_ds_ctor(ant_t *js, ant_value_t *args, int nargs) { } js_set_slot(obj, SLOT_ENTRIES, ts_obj); - js_set_slot(wrapper, SLOT_ENTRIES, ts_obj); - return obj; }