Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP a prototype crate design for ops #3025

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions op_hello/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "op_hello"
version = "0.1.0"
authors = ["Ryan Dahl <ry@tinyclouds.org>"]
edition = "2018"

[dependencies]
deno = "0.19.0"
deno_std = "0.19.0" # TODO deno_std is not yet published as crate

5 changes: 5 additions & 0 deletions op_hello/src/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const OP_HELLO = Deno.ops.add("hello");

export function hello() {
Deno.send(OP_HELLO);
}
19 changes: 19 additions & 0 deletions op_hello/src/hello_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TODO We need some way to import test modules.
// Attempt one:
//
// import { test } from "../../js/test_util.ts";
//
// Here it is referencing files across crate boundaries, which will break
// 'cargo package' and means the crate is not useable outside the deno tree.
// This might be okay for a first pass, but it's not the best solution.
//
// Attempt two:
// we invent a new URL for referencing files in other crates.
// this is magic and not browser compatible.. Browser compatibility for
// ops is not so important.
import { test } from "crate://deno_std@0.19.0/testing/mod.ts";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afinch7 is this along the lines of what you were thinking?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. It's very nice that all the relevant info is encoded into standard elements of the url structure. No need to design fancy parsing.

import { hello } from "./hello.ts";

test("hello test", () => {
hello();
});
30 changes: 30 additions & 0 deletions op_hello/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate deno;
use deno::*;

pub fn init(&mut isolate: Isolate) -> Result<(), ErrBox> {
isolate.register_op("hello", op_hello);

// TODO Somehow define register_deno_global
isolate.register_deno_global("src/hello.ts", "hello")
}

fn op_hello(_control_buf: &[u8], _zero_copy_buf: Option<PinnedBuf>) -> CoreOp {
println!("Hello world");
CoreOp::Sync(Box::new([]))
}

#[test]
fn hello_rust() {
match op_hello() {
CoreOp::Sync(buf) => {
assert_eq!(buf.len(), 0);
}
CoreOp::Async(_) => unreachable!(),
}
}

#[test]
fn hello_js() {
// TODO need to define run_js_test somehow...
run_js_test("src/hello_test.ts");
}