forked from shekohex/flutterust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
71 lines (63 loc) · 1.64 KB
/
lib.rs
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
#![allow(clippy::missing_safety_doc, clippy::not_unsafe_ptr_arg_deref)]
use allo_isolate::Isolate;
use ffi_helpers::null_pointer_check;
use lazy_static::lazy_static;
use std::{ffi::CStr, io, os::raw};
use tokio::runtime::{Builder, Runtime};
lazy_static! {
static ref RUNTIME: io::Result<Runtime> = Builder::new()
.threaded_scheduler()
.enable_all()
.core_threads(4)
.thread_name("flutterust")
.build();
}
macro_rules! error {
($result:expr) => {
error!($result, 0);
};
($result:expr, $error:expr) => {
match $result {
Ok(value) => value,
Err(e) => {
ffi_helpers::update_last_error(e);
return $error;
}
}
};
}
macro_rules! cstr {
($ptr:expr) => {
cstr!($ptr, 0)
};
($ptr:expr, $error:expr) => {{
null_pointer_check!($ptr);
error!(unsafe { CStr::from_ptr($ptr).to_str() }, $error)
}};
}
macro_rules! runtime {
() => {
match RUNTIME.as_ref() {
Ok(rt) => rt,
Err(_) => {
return 0;
}
}
};
}
#[no_mangle]
pub unsafe extern "C" fn last_error_length() -> i32 {
ffi_helpers::error_handling::last_error_length()
}
#[no_mangle]
pub unsafe extern "C" fn error_message_utf8(buf: *mut raw::c_char, length: i32) -> i32 {
ffi_helpers::error_handling::error_message_utf8(buf, length)
}
#[no_mangle]
pub extern "C" fn load_page(port: i64, url: *const raw::c_char) -> i32 {
let rt = runtime!();
let url = cstr!(url);
let t = Isolate::new(port).task(scrap::load_page(url));
rt.spawn(t);
1
}