summaryrefslogtreecommitdiff
path: root/src/libdeno.rs
blob: 35278a7dab692576559b5ec2fcb9d2f35d82469f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
use libc::c_char;
use libc::c_int;
use libc::c_void;
use std::ptr::null_mut;

#[repr(C)]
pub struct isolate {
  _unused: [u8; 0],
}

#[repr(C)]
#[derive(Clone, PartialEq)]
pub struct deno_buf {
  pub alloc_ptr: *mut u8,
  pub alloc_len: usize,
  pub data_ptr: *mut u8,
  pub data_len: usize,
}

impl deno_buf {
  pub fn empty() -> Self {
    Self {
      alloc_ptr: null_mut(),
      alloc_len: 0,
      data_ptr: null_mut(),
      data_len: 0,
    }
  }
}

/// Converts Rust &Buf to libdeno `deno_buf`.
impl<'a> From<&'a [u8]> for deno_buf {
  fn from(x: &'a [u8]) -> Self {
    Self {
      alloc_ptr: std::ptr::null_mut(),
      alloc_len: 0,
      data_ptr: x.as_ref().as_ptr() as *mut u8,
      data_len: x.len(),
    }
  }
}

type DenoRecvCb = unsafe extern "C" fn(
  user_data: *mut c_void,
  req_id: i32,
  buf: deno_buf,
  data_buf: deno_buf,
);

extern "C" {
  pub fn deno_init();
  pub fn deno_v8_version() -> *const c_char;
  pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
  pub fn deno_new(
    snapshot: deno_buf,
    shared: deno_buf,
    cb: DenoRecvCb,
  ) -> *const isolate;
  pub fn deno_delete(i: *const isolate);
  pub fn deno_last_exception(i: *const isolate) -> *const c_char;
  pub fn deno_check_promise_errors(i: *const isolate);
  pub fn deno_respond(
    i: *const isolate,
    user_data: *mut c_void,
    req_id: i32,
    buf: deno_buf,
  );
  pub fn deno_execute(
    i: *const isolate,
    user_data: *mut c_void,
    js_filename: *const c_char,
    js_source: *const c_char,
  ) -> c_int;
}