Skip to content

Commit

Permalink
feat(ops): optional OpState (denoland#13954)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Mar 15, 2022
1 parent 672f66d commit bd481bf
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 100 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ jobs:
~/.cargo/registry/index
~/.cargo/registry/cache
~/.cargo/git/db
key: 8-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
key: 9-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}

# In main branch, always creates fresh cache
- name: Cache build output (main)
Expand All @@ -260,7 +260,7 @@ jobs:
!./target/*/*.zip
!./target/*/*.tar.gz
key: |
8-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
9-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
# Restore cache from the latest 'main' branch build.
- name: Cache build output (PR)
Expand All @@ -276,7 +276,7 @@ jobs:
!./target/*/*.tar.gz
key: never_saved
restore-keys: |
8-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
9-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
# Don't save cache after building PRs or branches other than 'main'.
- name: Skip save cache (PR)
Expand Down
4 changes: 2 additions & 2 deletions bench_util/benches/op_baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ fn setup() -> Vec<Extension> {
}

#[op]
fn op_nop(_: &mut OpState) -> Result<(), AnyError> {
fn op_nop() -> Result<(), AnyError> {
Ok(())
}

#[op]
fn op_pi_json(_: &mut OpState) -> Result<i64, AnyError> {
fn op_pi_json() -> Result<i64, AnyError> {
Ok(314159)
}

Expand Down
4 changes: 2 additions & 2 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ fn create_compiler_snapshot(
}

#[op]
fn op_cwd(_state: &mut OpState, _args: Value) -> Result<Value, AnyError> {
fn op_cwd(_args: Value) -> Result<Value, AnyError> {
Ok(json!("cache:https:///"))
}

#[op]
fn op_exists(_state: &mut OpState, _args: Value) -> Result<Value, AnyError> {
fn op_exists(_args: Value) -> Result<Value, AnyError> {
Ok(json!(false))
}

Expand Down
10 changes: 2 additions & 8 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,12 @@ fn op_apply_source_map(
}

#[op]
fn op_format_diagnostic(
_state: &mut OpState,
args: Value,
) -> Result<Value, AnyError> {
fn op_format_diagnostic(args: Value) -> Result<Value, AnyError> {
let diagnostic: Diagnostics = serde_json::from_value(args)?;
Ok(json!(diagnostic.to_string()))
}

#[op]
fn op_format_file_name(
_state: &mut OpState,
file_name: String,
) -> Result<String, AnyError> {
fn op_format_file_name(file_name: String) -> Result<String, AnyError> {
Ok(format_file_name(&file_name))
}
5 changes: 1 addition & 4 deletions core/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use deno_core::RuntimeOptions;
use deno_core::*;

#[op]
fn op_sum(
_state: &mut OpState,
nums: Vec<f64>,
) -> Result<f64, deno_core::error::AnyError> {
fn op_sum(nums: Vec<f64>) -> Result<f64, deno_core::error::AnyError> {
// Sum inputs
let sum = nums.iter().fold(0.0, |a, v| a + v);
// return as a Result<f64, AnyError>
Expand Down
8 changes: 2 additions & 6 deletions core/ops_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn op_resources(
}

#[op]
pub fn op_void_sync(_state: &mut OpState) -> Result<(), Error> {
pub fn op_void_sync() -> Result<(), Error> {
Ok(())
}

Expand Down Expand Up @@ -101,11 +101,7 @@ pub fn op_metrics(

/// Builtin utility to print to stdout/stderr
#[op]
pub fn op_print(
_state: &mut OpState,
msg: String,
is_err: bool,
) -> Result<(), Error> {
pub fn op_print(msg: String, is_err: bool) -> Result<(), Error> {
if is_err {
stderr().write_all(msg.as_bytes())?;
stderr().flush().unwrap();
Expand Down
14 changes: 6 additions & 8 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ pub mod tests {
#[test]
fn test_error_builder() {
#[op]
fn op_err(_: &mut OpState) -> Result<(), Error> {
fn op_err() -> Result<(), Error> {
Err(custom_error("DOMExceptionOperationError", "abc"))
}

Expand Down Expand Up @@ -2533,9 +2533,7 @@ assertEquals(1, notify_return_value);
#[tokio::test]
async fn test_set_macrotask_callback_set_next_tick_callback() {
#[op]
async fn op_async_sleep(
_op_state: Rc<RefCell<OpState>>,
) -> Result<(), Error> {
async fn op_async_sleep() -> Result<(), Error> {
// Future must be Poll::Pending on first call
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
Ok(())
Expand Down Expand Up @@ -2610,13 +2608,13 @@ assertEquals(1, notify_return_value);
static NEXT_TICK: AtomicUsize = AtomicUsize::new(0);

#[op]
fn op_macrotask(_: &mut OpState) -> Result<(), AnyError> {
fn op_macrotask() -> Result<(), AnyError> {
MACROTASK.fetch_add(1, Ordering::Relaxed);
Ok(())
}

#[op]
fn op_next_tick(_: &mut OpState) -> Result<(), AnyError> {
fn op_next_tick() -> Result<(), AnyError> {
NEXT_TICK.fetch_add(1, Ordering::Relaxed);
Ok(())
}
Expand Down Expand Up @@ -2747,13 +2745,13 @@ assertEquals(1, notify_return_value);
static UNCAUGHT_EXCEPTION: AtomicUsize = AtomicUsize::new(0);

#[op]
fn op_promise_reject(_: &mut OpState) -> Result<(), AnyError> {
fn op_promise_reject() -> Result<(), AnyError> {
PROMISE_REJECT.fetch_add(1, Ordering::Relaxed);
Ok(())
}

#[op]
fn op_uncaught_exception(_: &mut OpState) -> Result<(), AnyError> {
fn op_uncaught_exception() -> Result<(), AnyError> {
UNCAUGHT_EXCEPTION.fetch_add(1, Ordering::Relaxed);
Ok(())
}
Expand Down
5 changes: 0 additions & 5 deletions ext/crypto/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::cell::RefCell;
use std::rc::Rc;

use crate::shared::*;
use aes::BlockEncrypt;
use aes::NewBlockCipher;
Expand All @@ -25,7 +22,6 @@ use deno_core::error::custom_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rsa::pkcs1::FromRsaPrivateKey;
use rsa::PaddingScheme;
Expand Down Expand Up @@ -79,7 +75,6 @@ pub enum DecryptAlgorithm {

#[op]
pub async fn op_crypto_decrypt(
_state: Rc<RefCell<OpState>>,
opts: DecryptOptions,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down
5 changes: 0 additions & 5 deletions ext/crypto/encrypt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::cell::RefCell;
use std::rc::Rc;

use crate::shared::*;

use aes::cipher::NewCipher;
Expand All @@ -27,7 +24,6 @@ use ctr::flavors::Ctr64BE;
use ctr::flavors::CtrFlavor;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rand::rngs::OsRng;
use rsa::pkcs1::FromRsaPublicKey;
Expand Down Expand Up @@ -83,7 +79,6 @@ pub enum EncryptAlgorithm {

#[op]
pub async fn op_crypto_encrypt(
_state: Rc<RefCell<OpState>>,
opts: EncryptOptions,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down
2 changes: 0 additions & 2 deletions ext/crypto/export_key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use deno_core::error::custom_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use rsa::pkcs1::UIntBytes;
use serde::Deserialize;
Expand Down Expand Up @@ -87,7 +86,6 @@ pub enum ExportKeyResult {

#[op]
pub fn op_crypto_export_key(
_state: &mut OpState,
opts: ExportKeyOptions,
key_data: RawKeyData,
) -> Result<ExportKeyResult, AnyError> {
Expand Down
5 changes: 0 additions & 5 deletions ext/crypto/generate_key.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;

use crate::shared::*;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use elliptic_curve::rand_core::OsRng;
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -44,7 +40,6 @@ pub enum GenerateKeyOptions {

#[op]
pub async fn op_crypto_generate_key(
_state: Rc<RefCell<OpState>>,
opts: GenerateKeyOptions,
) -> Result<ZeroCopyBuf, AnyError> {
let fun = || match opts {
Expand Down
2 changes: 0 additions & 2 deletions ext/crypto/import_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use elliptic_curve::pkcs8::der::Decodable as Pkcs8Decodable;
use elliptic_curve::pkcs8::PrivateKeyInfo;
Expand Down Expand Up @@ -90,7 +89,6 @@ pub enum ImportKeyResult {

#[op]
pub fn op_crypto_import_key(
_state: &mut OpState,
opts: ImportKeyOptions,
key_data: KeyData,
) -> Result<ImportKeyResult, AnyError> {
Expand Down
8 changes: 0 additions & 8 deletions ext/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use shared::operation_error;

use std::cell::RefCell;
use std::num::NonZeroU32;
use std::rc::Rc;

use p256::elliptic_curve::sec1::FromEncodedPoint;
use p256::pkcs8::FromPrivateKey;
Expand Down Expand Up @@ -169,7 +167,6 @@ pub struct SignArg {

#[op]
pub async fn op_crypto_sign_key(
_state: Rc<RefCell<OpState>>,
args: SignArg,
zero_copy: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down Expand Up @@ -324,7 +321,6 @@ pub struct VerifyArg {

#[op]
pub async fn op_crypto_verify_key(
_state: Rc<RefCell<OpState>>,
args: VerifyArg,
zero_copy: ZeroCopyBuf,
) -> Result<bool, AnyError> {
Expand Down Expand Up @@ -485,7 +481,6 @@ pub struct DeriveKeyArg {

#[op]
pub async fn op_crypto_derive_bits(
_state: Rc<RefCell<OpState>>,
args: DeriveKeyArg,
zero_copy: Option<ZeroCopyBuf>,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down Expand Up @@ -807,7 +802,6 @@ pub fn op_crypto_random_uuid(state: &mut OpState) -> Result<String, AnyError> {

#[op]
pub async fn op_crypto_subtle_digest(
_state: Rc<RefCell<OpState>>,
algorithm: CryptoHash,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand All @@ -831,7 +825,6 @@ pub struct WrapUnwrapKeyArg {

#[op]
pub fn op_crypto_wrap_key(
_state: &mut OpState,
args: WrapUnwrapKeyArg,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down Expand Up @@ -861,7 +854,6 @@ pub fn op_crypto_wrap_key(

#[op]
pub fn op_crypto_unwrap_key(
_state: &mut OpState,
args: WrapUnwrapKeyArg,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
Expand Down
6 changes: 1 addition & 5 deletions ext/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,13 @@ fn ffi_call(args: FfiCallArgs, symbol: &Symbol) -> Result<Value, AnyError> {
}

#[op]
fn op_ffi_call_ptr(
_state: &mut deno_core::OpState,
args: FfiCallPtrArgs,
) -> Result<Value, AnyError> {
fn op_ffi_call_ptr(args: FfiCallPtrArgs) -> Result<Value, AnyError> {
let symbol = args.get_symbol();
ffi_call(args.into(), &symbol)
}

#[op]
async fn op_ffi_call_ptr_nonblocking(
_state: Rc<RefCell<deno_core::OpState>>,
args: FfiCallPtrArgs,
) -> Result<Value, AnyError> {
let symbol = args.get_symbol();
Expand Down
5 changes: 1 addition & 4 deletions ext/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,7 @@ async fn op_http_read(
}

#[op]
fn op_http_websocket_accept_header(
_: &mut OpState,
key: String,
) -> Result<String, AnyError> {
fn op_http_websocket_accept_header(key: String) -> Result<String, AnyError> {
let digest = ring::digest::digest(
&ring::digest::SHA1_FOR_LEGACY_USE_ONLY,
format!("{}258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key).as_bytes(),
Expand Down
4 changes: 0 additions & 4 deletions ext/url/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type UrlParts = String;
/// optional part to "set" after parsing. Return `UrlParts`.
#[op]
pub fn op_url_parse(
_state: &mut deno_core::OpState,
href: String,
base_href: Option<String>,
) -> Result<UrlParts, AnyError> {
Expand Down Expand Up @@ -92,7 +91,6 @@ pub enum UrlSetter {

#[op]
pub fn op_url_reparse(
_state: &mut deno_core::OpState,
href: String,
setter_opts: (UrlSetter, String),
) -> Result<UrlParts, AnyError> {
Expand Down Expand Up @@ -162,7 +160,6 @@ fn url_result(

#[op]
pub fn op_url_parse_search_params(
_state: &mut deno_core::OpState,
args: Option<String>,
zero_copy: Option<ZeroCopyBuf>,
) -> Result<Vec<(String, String)>, AnyError> {
Expand All @@ -182,7 +179,6 @@ pub fn op_url_parse_search_params(

#[op]
pub fn op_url_stringify_search_params(
_state: &mut deno_core::OpState,
args: Vec<(String, String)>,
) -> Result<String, AnyError> {
let search = form_urlencoded::Serializer::new(String::new())
Expand Down
2 changes: 0 additions & 2 deletions ext/url/urlpattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use urlpattern::quirks::UrlPattern;

#[op]
pub fn op_urlpattern_parse(
_state: &mut deno_core::OpState,
input: StringOrInit,
base_url: Option<String>,
) -> Result<UrlPattern, AnyError> {
Expand All @@ -27,7 +26,6 @@ pub fn op_urlpattern_parse(

#[op]
pub fn op_urlpattern_process_match_input(
_state: &mut deno_core::OpState,
input: StringOrInit,
base_url: Option<String>,
) -> Result<Option<(MatchInput, quirks::Inputs)>, AnyError> {
Expand Down
Loading

0 comments on commit bd481bf

Please sign in to comment.