summaryrefslogtreecommitdiff
path: root/core/examples/disable_ops.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-08-31 13:08:16 +0200
committerGitHub <noreply@github.com>2021-08-31 13:08:16 +0200
commitb518f5e1ba4c3281d4aee4c637f410db382ce751 (patch)
treea10f196368db50473a99a50822fb4d8513468734 /core/examples/disable_ops.rs
parentcee5be45394c77863dacbf8f1d0f0b90188d2cca (diff)
feat(core): facilitate op-disabling middleware (#11858)
* feat(core): facilitate op-disabling middleware By providing `void_op_sync()` and `void_op_async() as well as `core/examples/disable_ops.rs`
Diffstat (limited to 'core/examples/disable_ops.rs')
-rw-r--r--core/examples/disable_ops.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/core/examples/disable_ops.rs b/core/examples/disable_ops.rs
new file mode 100644
index 000000000..2d28908bf
--- /dev/null
+++ b/core/examples/disable_ops.rs
@@ -0,0 +1,27 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+//! This example shows you how to define ops in Rust and then call them from
+//! JavaScript.
+
+use deno_core::Extension;
+use deno_core::JsRuntime;
+use deno_core::RuntimeOptions;
+
+fn main() {
+ let my_ext = Extension::builder()
+ .middleware(|name, opfn| match name {
+ "op_print" => deno_core::void_op_sync(),
+ _ => opfn,
+ })
+ .build();
+
+ // Initialize a runtime instance
+ let mut runtime = JsRuntime::new(RuntimeOptions {
+ extensions: vec![my_ext],
+ ..Default::default()
+ });
+
+ // Deno.core.print() will now be a NOP
+ runtime
+ .execute_script("<usage>", r#"Deno.core.print("I'm broken")"#)
+ .unwrap();
+}