1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor: don't expose ops to user code (#22273)

Brings the number of ops exposed to user code down to 58.
This commit is contained in:
Bartek Iwańczuk 2024-02-06 00:26:36 +01:00 committed by GitHub
parent 2c621f5894
commit b94dabdb81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 150 deletions

View file

@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const EXPECTED_OP_COUNT = 158;
const EXPECTED_OP_COUNT = 58;
Deno.test(function checkExposedOps() {
// @ts-ignore TS doesn't allow to index with symbol

View file

@ -183,9 +183,9 @@ function makeStreamWithCount(
Deno.test(async function readableStream() {
const rid = resourceForReadableStream(helloWorldStream());
const buffer = new Uint8Array(1024);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 12);
core.ops.op_close(rid);
core.close(rid);
});
// Close the stream after reading everything
@ -195,9 +195,9 @@ Deno.test(async function readableStreamClose() {
helloWorldStream(false, cancel.resolve),
);
const buffer = new Uint8Array(1024);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 12);
core.ops.op_close(rid);
core.close(rid);
assertEquals(await cancel.promise, "resource closed");
});
@ -208,9 +208,9 @@ Deno.test(async function readableStreamClosePartialRead() {
helloWorldStream(false, cancel.resolve),
);
const buffer = new Uint8Array(5);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 5);
core.ops.op_close(rid);
core.close(rid);
assertEquals(await cancel.promise, "resource closed");
});
@ -220,7 +220,7 @@ Deno.test(async function readableStreamCloseWithoutRead() {
const rid = resourceForReadableStream(
helloWorldStream(false, cancel.resolve),
);
core.ops.op_close(rid);
core.close(rid);
assertEquals(await cancel.promise, "resource closed");
});
@ -228,54 +228,54 @@ Deno.test(async function readableStreamCloseWithoutRead() {
Deno.test(async function readableStreamCloseWithoutRead2() {
const cancel = Promise.withResolvers();
const rid = resourceForReadableStream(longAsyncStream(cancel.resolve));
core.ops.op_close(rid);
core.close(rid);
assertEquals(await cancel.promise, "resource closed");
});
Deno.test(async function readableStreamPartial() {
const rid = resourceForReadableStream(helloWorldStream());
const buffer = new Uint8Array(5);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 5);
const buffer2 = new Uint8Array(1024);
const nread2 = await core.ops.op_read(rid, buffer2);
const nread2 = await core.read(rid, buffer2);
assertEquals(nread2, 7);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamLongReadAll() {
const rid = resourceForReadableStream(longStream());
const buffer = await core.ops.op_read_all(rid);
const buffer = await core.readAll(rid);
assertEquals(buffer.length, LOREM.length * 4);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamLongAsyncReadAll() {
const rid = resourceForReadableStream(longAsyncStream());
const buffer = await core.ops.op_read_all(rid);
const buffer = await core.readAll(rid);
assertEquals(buffer.length, LOREM.length * 100);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamVeryLongReadAll() {
const rid = resourceForReadableStream(veryLongTinyPacketStream(1_000_000));
const buffer = await core.ops.op_read_all(rid);
const buffer = await core.readAll(rid);
assertEquals(buffer.length, 1_000_000);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamLongByPiece() {
const rid = resourceForReadableStream(longStream());
let total = 0;
for (let i = 0; i < 100; i++) {
const length = await core.ops.op_read(rid, new Uint8Array(16));
const length = await core.read(rid, new Uint8Array(16));
total += length;
if (length == 0) {
break;
}
}
assertEquals(total, LOREM.length * 4);
core.ops.op_close(rid);
core.close(rid);
});
for (
@ -289,57 +289,57 @@ for (
const rid = resourceForReadableStream(errorStream(type));
let nread;
try {
nread = await core.ops.op_read(rid, new Uint8Array(16));
nread = await core.read(rid, new Uint8Array(16));
} catch (_) {
fail("Should not have thrown");
}
assertEquals(12, nread);
try {
await core.ops.op_read(rid, new Uint8Array(1));
await core.read(rid, new Uint8Array(1));
fail();
} catch (e) {
assertEquals(e.message, `Uh oh (${type})!`);
}
core.ops.op_close(rid);
core.close(rid);
});
}
Deno.test(async function readableStreamEmptyOnStart() {
const rid = resourceForReadableStream(emptyStream(true));
const buffer = new Uint8Array(1024);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 0);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamEmptyOnPull() {
const rid = resourceForReadableStream(emptyStream(false));
const buffer = new Uint8Array(1024);
const nread = await core.ops.op_read(rid, buffer);
const nread = await core.read(rid, buffer);
assertEquals(nread, 0);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamEmptyReadAll() {
const rid = resourceForReadableStream(emptyStream(false));
const buffer = await core.ops.op_read_all(rid);
const buffer = await core.readAll(rid);
assertEquals(buffer.length, 0);
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamWithEmptyChunk() {
const rid = resourceForReadableStream(emptyChunkStream());
const buffer = await core.ops.op_read_all(rid);
const buffer = await core.readAll(rid);
assertEquals(buffer, new Uint8Array([1, 2]));
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamWithEmptyChunkOneByOne() {
const rid = resourceForReadableStream(emptyChunkStream());
assertEquals(1, await core.ops.op_read(rid, new Uint8Array(1)));
assertEquals(1, await core.ops.op_read(rid, new Uint8Array(1)));
assertEquals(0, await core.ops.op_read(rid, new Uint8Array(1)));
core.ops.op_close(rid);
assertEquals(1, await core.read(rid, new Uint8Array(1)));
assertEquals(1, await core.read(rid, new Uint8Array(1)));
assertEquals(0, await core.read(rid, new Uint8Array(1)));
core.close(rid);
});
// Ensure that we correctly transmit all the sub-chunks of the larger chunks.
@ -348,7 +348,7 @@ Deno.test(async function readableStreamReadSmallerChunks() {
const rid = resourceForReadableStream(largePacketStream(packetSize, 1));
const buffer = new Uint8Array(packetSize);
for (let i = 0; i < packetSize / 1024; i++) {
await core.ops.op_read(rid, buffer.subarray(i * 1024, i * 1024 + 1024));
await core.read(rid, buffer.subarray(i * 1024, i * 1024 + 1024));
}
for (let i = 0; i < 256; i++) {
assertEquals(
@ -357,7 +357,7 @@ Deno.test(async function readableStreamReadSmallerChunks() {
`at index ${i * (packetSize / 256)}`,
);
}
core.ops.op_close(rid);
core.close(rid);
});
Deno.test(async function readableStreamLargePackets() {
@ -365,7 +365,7 @@ Deno.test(async function readableStreamLargePackets() {
const rid = resourceForReadableStream(largePacketStream(packetSize, 1024));
for (let i = 0; i < 1024; i++) {
const buffer = new Uint8Array(packetSize);
assertEquals(packetSize, await core.ops.op_read(rid, buffer));
assertEquals(packetSize, await core.read(rid, buffer));
for (let i = 0; i < 256; i++) {
assertEquals(
i,
@ -374,8 +374,8 @@ Deno.test(async function readableStreamLargePackets() {
);
}
}
assertEquals(0, await core.ops.op_read(rid, new Uint8Array(1)));
core.ops.op_close(rid);
assertEquals(0, await core.read(rid, new Uint8Array(1)));
core.close(rid);
});
Deno.test(async function readableStreamVeryLargePackets() {
@ -385,7 +385,7 @@ Deno.test(async function readableStreamVeryLargePackets() {
// Read 96kB up to 12,288 times (96kB is not an even multiple of the 1MB packet size to test this)
const readCounts: Record<number, number> = {};
for (let i = 0; i < 12 * 1024; i++) {
const nread = await core.ops.op_read(rid, new Uint8Array(96 * 1024));
const nread = await core.read(rid, new Uint8Array(96 * 1024));
total += nread;
readCounts[nread] = (readCounts[nread] || 0) + 1;
if (nread == 0) {
@ -394,7 +394,7 @@ Deno.test(async function readableStreamVeryLargePackets() {
}
assertEquals({ 0: 1, 65536: 1024, 98304: 10 * 1024 }, readCounts);
assertEquals(total, 1024 * 1024 * 1024);
core.ops.op_close(rid);
core.close(rid);
});
for (const count of [0, 1, 2, 3]) {
@ -420,12 +420,12 @@ function createStreamTest(
);
for (let i = 0; i < count; i++) {
const buffer = new Uint8Array(1);
await core.ops.op_read(rid, buffer);
await core.read(rid, buffer);
}
if (action == "Throw") {
try {
const buffer = new Uint8Array(1);
assertEquals(1, await core.ops.op_read(rid, buffer));
assertEquals(1, await core.read(rid, buffer));
fail();
} catch (e) {
// We expect this to be thrown
@ -433,10 +433,10 @@ function createStreamTest(
}
} else {
const buffer = new Uint8Array(1);
assertEquals(0, await core.ops.op_read(rid, buffer));
assertEquals(0, await core.read(rid, buffer));
}
} finally {
core.ops.op_close(rid);
core.close(rid);
}
});
}
@ -467,7 +467,7 @@ for (const packetCount of [1, 1024]) {
);
try {
for (let i = 0; i < packetCount; i++) {
await core.ops.op_read(rid, new Uint8Array(1));
await core.read(rid, new Uint8Array(1));
}
fail();
} catch (e) {

View file

@ -538,34 +538,12 @@ function exposeUnstableFeaturesForWindowOrWorkerGlobalScope(options) {
// NOTE(bartlomieju): remove all the ops that have already been imported using
// "virtual op module" (`ext:core/ops`).
const NOT_IMPORTED_OPS = [
"op_abort_wasm_streaming",
"op_add_async",
"op_add",
"op_apply_source_map_filename",
"op_apply_source_map",
"op_bench_now",
"op_bootstrap_args",
"op_bootstrap_is_tty",
"op_bootstrap_no_color",
"op_bootstrap_pid",
"op_broadcast_unsubscribe",
"op_can_write_vectored",
"op_close",
"op_cpus",
"op_create_brotli_compress",
"op_create_brotli_decompress",
"op_current_user_call_site",
"op_decode",
"op_deserialize",
"op_destructure_error",
"op_dispatch_bench_event",
"op_dispatch_exception",
"op_encode_binary_string",
"op_encode",
"op_error_async_deferred",
"op_error_async",
"op_eval_context",
"op_event_loop_has_more_work",
"op_ffi_buf_copy_into",
"op_ffi_call_nonblocking",
"op_ffi_call_ptr_nonblocking",
@ -595,86 +573,18 @@ const NOT_IMPORTED_OPS = [
"op_ffi_unsafe_callback_close",
"op_ffi_unsafe_callback_create",
"op_ffi_unsafe_callback_ref",
"op_format_file_name",
"op_get_promise_details",
"op_get_proxy_details",
"op_has_tick_scheduled",
"op_http_get_request_header",
"op_http2_accept",
"op_http2_client_end_stream",
"op_http2_client_get_response_body_chunk",
"op_http2_client_get_response_trailers",
"op_http2_client_get_response",
"op_http2_client_request",
"op_http2_client_reset_stream",
"op_http2_client_send_data",
"op_http2_client_send_trailers",
"op_http2_connect",
"op_http2_listen",
"op_http2_poll_client_connection",
"op_http2_send_response",
"op_image_decode_png",
"op_image_process",
"op_is_any_array_buffer",
"op_is_arguments_object",
"op_is_array_buffer_view",
"op_is_array_buffer",
"op_is_async_function",
"op_is_big_int_object",
"op_is_boolean_object",
"op_is_boxed_primitive",
"op_is_data_view",
"op_is_date",
"op_is_generator_function",
"op_is_generator_object",
"op_is_map_iterator",
"op_is_map",
"op_is_module_namespace_object",
"op_is_native_error",
"op_is_number_object",
"op_is_promise",
"op_is_proxy",
"op_is_reg_exp",
"op_is_set_iterator",
"op_is_set",
"op_is_shared_array_buffer",
"op_is_string_object",
"op_is_symbol_object",
"op_is_typed_array",
"op_is_weak_map",
"op_is_weak_set",
"op_main_module",
"op_memory_usage",
"op_napi_open",
"op_npm_process_state",
"op_op_names",
"op_panic",
"op_pledge_test_permissions",
"op_ppid",
"op_print",
"op_queue_microtask",
"op_raw_write_vectored",
"op_read_all",
"op_read_sync",
"op_read",
"op_ref_op",
"op_register_bench",
"op_register_test_step",
"op_register_test",
"op_resources",
"op_restore_test_permissions",
"op_run_microtasks",
"op_serialize",
"op_set_exit_code",
"op_set_format_exception_callback",
"op_set_handled_promise_rejection_handler",
"op_set_has_tick_scheduled",
"op_set_promise_hooks",
"op_set_wasm_streaming_callback",
"op_shutdown",
"op_snapshot_options",
"op_spawn_child",
"op_str_byte_length",
"op_test_event_step_result_failed",
"op_test_event_step_result_ignored",
"op_test_event_step_result_ok",
@ -683,15 +593,6 @@ const NOT_IMPORTED_OPS = [
"op_test_op_sanitizer_finish",
"op_test_op_sanitizer_get_async_message",
"op_test_op_sanitizer_report",
"op_timer_cancel",
"op_timer_queue",
"op_timer_ref",
"op_timer_unref",
"op_try_close",
"op_unref_op",
"op_v8_cached_data_version_tag",
"op_v8_get_heap_statistics",
"op_vm_run_in_new_context",
"op_void_async",
"op_void_sync",
"op_worker_close",
@ -699,12 +600,11 @@ const NOT_IMPORTED_OPS = [
"op_worker_post_message",
"op_worker_recv_message",
"op_worker_sync_fetch",
"op_write_all",
"op_write_sync",
"op_write_type_error",
"op_write",
"op_ws_send_pong",
"op_jupyter_broadcast",
"op_format_file_name",
"op_apply_source_map",
"op_apply_source_map_filename",
];
function removeImportedOps() {