Skip to content

Commit

Permalink
Merge pull request #32 from sagudev/master
Browse files Browse the repository at this point in the history
Updated to latest rust and mozjs
  • Loading branch information
ricochet1k authored Jan 16, 2020
2 parents 2838ab9 + 2834d5d commit d124c02
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 221 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Rust

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Prepare
run: |
rustup update
sudo apt update
wget -q https://hg.mozilla.org/mozilla-central/raw-file/default/python/mozboot/bin/bootstrap.py -O bootstrap.py
python bootstrap.py --application-choice=browser --no-interactive|| exit 0
- name: Build
run: |
export SHELL=/bin/bash
cargo build --verbose
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Run tests
run: cargo test --verbose
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
.tags
mach
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
[package]
name = "rjs"
version = "0.1.0"
edition = "2018"
authors = ["Matt Peterson <[email protected]>"]

[features]
debugmozjs = ['mozjs/debugmozjs']

[dependencies]
mozjs = {git = "https://github.com/servo/rust-mozjs", features = ["promises"]}
tokio-core = "0.1.6"
tokio-timer = "0.1.1"
futures = "0.1"
mozjs = {git = "https://github.com/servo/rust-mozjs"}
tokio-core = "0.1.17"
tokio-timer = "0.2.12"
futures = "0.1.29"
# concat_bytes = "0.1.0"
lazy_static = "1.0.0"
slab = "0.4.0"
downcast = "0.9.1"
glutin = "*"
gl = "*"
libc = "0.2.34"
lazy_static = "1.4.0"
slab = "0.4.2"
downcast = "0.10.0"
glutin = "0.16.0"
gl = "0.14.0"
libc = "0.2.66"

[package.metadata.android]
label = "Using assets android-rs-glue example"
Expand Down
20 changes: 10 additions & 10 deletions src/jslib/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use jslib::eventloop;
use crate::jslib::eventloop;
use mozjs::jsapi::{HandleObject, JSContext, JSObject, JS_GetCompartmentPrivate,
JS_SetCompartmentPrivate};
use mozjs::rust::get_context_compartment;
JS_SetCompartmentPrivate, GetRealmPrivate, SetRealmPrivate};
use mozjs::jsapi::GetCurrentRealmOrNull;

use std::collections::HashMap;
use std::any::TypeId;
Expand Down Expand Up @@ -52,27 +52,27 @@ pub type RJSRemote = eventloop::Remote<RJSContext>;
pub type RuntimePrivate = eventloop::WeakHandle<RJSContext>;

pub fn store_private(cx: *mut JSContext, handle: &RJSHandle) {
let compartment = unsafe { get_context_compartment(cx) };
let compartment = unsafe { GetCurrentRealmOrNull(cx) };
let private = Box::new(handle.downgrade());
unsafe {
JS_SetCompartmentPrivate(compartment, Box::into_raw(private) as *mut c_void);
SetRealmPrivate(compartment, Box::into_raw(private) as *mut c_void);
}
}

pub fn clear_private(cx: *mut JSContext) {
let compartment = unsafe { get_context_compartment(cx) };
let private = unsafe { JS_GetCompartmentPrivate(compartment) as *mut RuntimePrivate };
let compartment = unsafe { GetCurrentRealmOrNull(cx) };
let private = unsafe { GetRealmPrivate(compartment) as *mut RuntimePrivate };
if !private.is_null() {
unsafe {
let _ = Box::from_raw(private);
JS_SetCompartmentPrivate(compartment, ptr::null_mut());
SetRealmPrivate(compartment, ptr::null_mut());
}
}
}

pub fn get_handle(cx: *mut JSContext) -> Option<RJSHandle> {
let compartment = unsafe { get_context_compartment(cx) };
let private = unsafe { JS_GetCompartmentPrivate(compartment) as *const RuntimePrivate };
let compartment = unsafe { GetCurrentRealmOrNull(cx) };
let private = unsafe { GetRealmPrivate(compartment) as *const RuntimePrivate };
if private.is_null() {
None
} else {
Expand Down
26 changes: 12 additions & 14 deletions src/jslib/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ use std::cell::RefCell;
use std::fmt::Debug;
use downcast::Any;
use mozjs::rust::{GCMethods, Runtime, Trace};
use mozjs::jsapi::{GCForReason, Heap, JSGCInvocationKind, JSTracer, JS_AddExtraGCRootsTracer,
JS_RemoveExtraGCRootsTracer, Reason};
use mozjs::jsapi::{GCReason, Heap, JSGCInvocationKind, JSTracer, JS_AddExtraGCRootsTracer,
JS_RemoveExtraGCRootsTracer, NonIncrementalGC};

use std::os::raw::c_void;
use std::boxed::FnBox;

//type EventLoopFn<T> = for<'t> Fn(&'t T, Handle<T>);
type Message<T> = (Remote<T>, Box<FnBox(Handle<T>) -> ()>);
type Message<T> = (Remote<T>, Box<FnOnce(Handle<T>) -> ()>);

type RefSlab = RefCell<Slab<RefSlabEl>>;
type RefSlabEl = RefCell<Option<Box<Traceable>>>;
Expand All @@ -34,7 +33,7 @@ trait Traceable: Any {
fn get_trace(&self) -> &Trace;
}

unsafe impl Trace for Traceable {
unsafe impl Trace for dyn Traceable {
unsafe fn trace(&self, trc: *mut JSTracer) {
self.get_trace().trace(trc)
}
Expand Down Expand Up @@ -91,7 +90,7 @@ where
};

let extradata: *mut rc::Weak<RefSlab> = Box::into_raw(Box::new(rc::Weak::clone(&handle.slab)));
unsafe { JS_AddExtraGCRootsTracer(rt.rt(), Some(ref_slab_tracer), extradata as *mut _) };
unsafe { JS_AddExtraGCRootsTracer(rt.cx(), Some(ref_slab_tracer), extradata as *mut _) };

let _: Result<(), ()> = core.run(future::lazy(|| {
first(handle);
Expand All @@ -104,15 +103,15 @@ where
data: Rc::clone(&data),
slab: Rc::downgrade(&slab),
};
unsafe { GCForReason(rt.rt(), JSGCInvocationKind::GC_SHRINK, Reason::NO_REASON) };
f.call_box((handle,));
unsafe { GCForReason(rt.rt(), JSGCInvocationKind::GC_SHRINK, Reason::NO_REASON) };
unsafe { NonIncrementalGC(rt.cx(), JSGCInvocationKind::GC_SHRINK, GCReason::NO_REASON) };
f(handle);
unsafe { NonIncrementalGC(rt.cx(), JSGCInvocationKind::GC_SHRINK, GCReason::NO_REASON) };
Ok(())
})
}));

unsafe {
JS_RemoveExtraGCRootsTracer(rt.rt(), Some(ref_slab_tracer), extradata as *mut _);
JS_RemoveExtraGCRootsTracer(rt.cx(), Some(ref_slab_tracer), extradata as *mut _);
Box::from_raw(extradata);
}
}
Expand Down Expand Up @@ -187,8 +186,7 @@ impl<T> Handle<T> {
marker: PhantomData,
}
}

pub fn retrieve<V: Debug + 'static>(&self, rref: &RemoteRef<V>) -> Option<V> {
pub fn retrieve<V: Copy + 'static>(&self, rref: &RemoteRef<V>) -> Option<V> {
let slab = self.slab.upgrade().unwrap();
let slab = slab.borrow();
let r = unsafe { slab.get_unchecked(rref.key) };
Expand Down Expand Up @@ -259,8 +257,8 @@ impl<T> Remote<T> {
F: FnOnce(Handle<T>) + Send + 'static,
{
let me: Remote<T> = (*self).clone();
let myfunc: Box<FnBox(Handle<T>) -> () + 'static> = Box::new(f);
//let myfunc: Box<FnBox<T>> = Box::new( |a, b| f(a, b) );
let myfunc: Box<FnOnce(Handle<T>) -> () + 'static> = Box::new(f);
//let myfunc: Box<FnOnce<T>> = Box::new( |a, b| f(a, b) );
let fb = (me, myfunc);
(*self.0).unbounded_send(fb).unwrap()
}
Expand Down
Loading

0 comments on commit d124c02

Please sign in to comment.