Skip to content

Commit

Permalink
refactor InitContext
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Nov 20, 2019
1 parent 112622d commit cb94110
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
5 changes: 2 additions & 3 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as minimal from "./dispatch_minimal.ts";
import * as json from "./dispatch_json.ts";
import { AsyncHandler } from "./native_plugins.ts";

// These consts are shared with Rust. Update with care.
export let OP_READ: number;
Expand Down Expand Up @@ -69,11 +68,11 @@ export let OP_DIAL_TLS: number;
export let OP_HOSTNAME: number;
export let OP_OPEN_NATIVE_PLUGIN: number;

const NATIVE_PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();
const NATIVE_PLUGIN_ASYNC_HANDLER_MAP: Map<number, MessageCallback> = new Map();

export function setPluginAsyncHandler(
opId: number,
handler: AsyncHandler
handler: MessageCallback
): void {
NATIVE_PLUGIN_ASYNC_HANDLER_MAP.set(opId, handler);
}
Expand Down
8 changes: 2 additions & 6 deletions cli/js/native_plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import { sendSync } from "./dispatch_json.ts";
import { OP_OPEN_NATIVE_PLUGIN, setPluginAsyncHandler } from "./dispatch.ts";
import { core } from "./core.ts";

export interface AsyncHandler {
(opId: number, msg: Uint8Array): void;
}

interface NativePluginOp {
dispatch(
control: Uint8Array,
zeroCopy?: ArrayBufferView | null
): Uint8Array | null;
setAsyncHandler(handler: AsyncHandler): void;
setAsyncHandler(handler: MessageCallback): void;
}

class NativePluginOpImpl implements NativePluginOp {
Expand All @@ -24,7 +20,7 @@ class NativePluginOpImpl implements NativePluginOp {
return core.dispatch(this.opId, control, zeroCopy);
}

setAsyncHandler(handler: AsyncHandler): void {
setAsyncHandler(handler: MessageCallback): void {
setPluginAsyncHandler(this.opId, handler);
}
}
Expand Down
42 changes: 17 additions & 25 deletions cli/ops/native_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,22 @@ fn open_plugin<P: AsRef<OsStr>>(lib_path: P) -> Result<Library, ErrBox> {

struct NativePluginResource {
lib: Library,
ops: Vec<(String, OpId)>,
ops: HashMap<String, OpId>,
}

impl Resource for NativePluginResource {}

struct InitContext {
registry: Arc<deno::OpRegistry>,
plugin_rid: ResourceId,
ops: Vec<(String, OpId)>,
ops: HashMap<String, Box<OpDispatcher>>,
}

impl PluginInitContext for InitContext {
fn register_op(
&mut self,
name: &str,
op: Box<dyn Fn(&[u8], Option<PinnedBuf>) -> CoreOp + Send + Sync + 'static>,
) -> OpId {
let opid = self
.registry
.register(&format!("{}_{}", self.plugin_rid, name), op);
self.ops.push((name.to_string(), opid));
opid
fn register_op(&mut self, name: &str, op: Box<OpDispatcher>) {
let existing = self.ops.insert(name.to_string(), op);
assert!(
existing.is_none(),
format!("Op already registered: {}", name)
);
}
}

Expand All @@ -71,7 +65,7 @@ pub fn op_open_native_plugin(
let lib = open_plugin(filename)?;
let plugin_resource = NativePluginResource {
lib,
ops: Vec::new(),
ops: HashMap::new(),
};
let mut table = state.lock_resource_table();
let rid = table.add("native_plugin", Box::new(plugin_resource));
Expand All @@ -83,17 +77,15 @@ pub fn op_open_native_plugin(
.symbol::<PluginInitFn>("native_plugin_init")
}?;
let mut init_context = InitContext {
registry: registry.clone(),
plugin_rid: rid,
ops: Vec::new(),
ops: HashMap::new(),
};
init_fn(&mut init_context);
plugin_resource.ops.append(&mut init_context.ops);
let ops: HashMap<String, OpId> = plugin_resource
.ops
.iter()
.map(|record| (record.0.clone(), record.1))
.collect();
for op in init_context.ops {
let op_id = registry.register(&op.0, op.1);
plugin_resource.ops.insert(op.0, op_id);
}

Ok(JsonOp::Sync(json!({ "rid": rid, "ops": ops })))
Ok(JsonOp::Sync(
json!({ "rid": rid, "ops": plugin_resource.ops }),
))
}
2 changes: 1 addition & 1 deletion core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type CoreError = ();
pub type CoreOp = Op<CoreError>;

/// Main type describing op
type OpDispatcher =
pub type OpDispatcher =
dyn Fn(&[u8], Option<PinnedBuf>) -> CoreOp + Send + Sync + 'static;

#[derive(Default)]
Expand Down
4 changes: 2 additions & 2 deletions core/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::libdeno::{OpId, PinnedBuf};
use crate::libdeno::PinnedBuf;
use crate::ops::CoreOp;

pub type PluginInitFn = fn(context: &mut dyn PluginInitContext);
Expand All @@ -8,7 +8,7 @@ pub trait PluginInitContext {
&mut self,
name: &str,
op: Box<dyn Fn(&[u8], Option<PinnedBuf>) -> CoreOp + Send + Sync + 'static>,
) -> OpId;
);
}

#[macro_export]
Expand Down

0 comments on commit cb94110

Please sign in to comment.