Skip to content

Commit

Permalink
Merge pull request #27 from EPashkin/add_new_types
Browse files Browse the repository at this point in the history
Add stub for JSCValue and JSCContext
  • Loading branch information
antoyo authored Mar 26, 2019
2 parents 42e5205 + 86c3715 commit 973e7ba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: xenial
language: rust
rust:
- nightly
Expand All @@ -10,6 +11,7 @@ addons:
apt:
packages:
- libgtk-3-dev
- libmount-dev
script:
- rustc --version
- cargo build
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ name = "javascriptcore"
git = "https://github.com/gtk-rs/glib"

[dependencies.javascriptcore-rs-sys]
git = "https://github.com/gtk-rs/javascriptcore-rs"
path = "./javascriptcore-sys"
version = "0.1.1"
2 changes: 2 additions & 0 deletions javascriptcore-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ extern crate libc;

use libc::{c_char, c_double, c_void};

pub type JSCValue = *mut c_void;
pub type JSCContext = *mut c_void;
pub type JSGlobalContextRef = *mut c_void;
pub type JSValueRef = *mut c_void;
pub type JSStringRef = *mut c_void;
Expand Down
57 changes: 35 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,52 @@ use std::ptr;
use glib::translate::{FromGlibPtrFull, FromGlibPtrNone};
use javascriptcore_sys::*;

pub struct GlobalContext {
pub struct GlobalContextRef {
raw: JSGlobalContextRef,
}

pub struct Value {
pub struct ValueRef {
raw: JSValueRef,
}

impl Value {
pub fn is_boolean(&self, context: &GlobalContext) -> bool {
pub struct Value {
raw: JSCValue,
}

impl ValueRef {
pub fn is_boolean(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsBoolean(context.raw, self.raw) != 0 }
}

pub fn is_null(&self, context: &GlobalContext) -> bool {
pub fn is_null(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsNull(context.raw, self.raw) != 0 }
}

pub fn is_undefined(&self, context: &GlobalContext) -> bool {
pub fn is_undefined(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsUndefined(context.raw, self.raw) != 0 }
}

pub fn is_number(&self, context: &GlobalContext) -> bool {
pub fn is_number(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsNumber(context.raw, self.raw) != 0 }
}

pub fn is_string(&self, context: &GlobalContext) -> bool {
pub fn is_string(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsString(context.raw, self.raw) != 0 }
}

pub fn is_object(&self, context: &GlobalContext) -> bool {
pub fn is_object(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsObject(context.raw, self.raw) != 0 }
}

pub fn is_array(&self, context: &GlobalContext) -> bool {
pub fn is_array(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsArray(context.raw, self.raw) != 0 }
}

pub fn is_date(&self, context: &GlobalContext) -> bool {
pub fn is_date(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueIsDate(context.raw, self.raw) != 0 }
}

pub fn to_number(&self, context: &GlobalContext) -> Option<f64> {
pub fn to_number(&self, context: &GlobalContextRef) -> Option<f64> {
let mut exception = ptr::null_mut();
let result = unsafe { JSValueToNumber(context.raw, self.raw, &mut exception) };
if exception.is_null() {
Expand All @@ -61,11 +65,11 @@ impl Value {
}
}

pub fn to_boolean(&self, context: &GlobalContext) -> bool {
pub fn to_boolean(&self, context: &GlobalContextRef) -> bool {
unsafe { JSValueToBoolean(context.raw, self.raw) != 0}
}

pub fn to_string(&self, context: &GlobalContext) -> Option<String> {
pub fn to_string(&self, context: &GlobalContextRef) -> Option<String> {
unsafe {
let mut exception = ptr::null_mut();
let jsstring = JSValueToStringCopy(context.raw, self.raw, &mut exception);
Expand All @@ -84,33 +88,42 @@ impl Value {
}
}

impl FromGlibPtrNone<JSValueRef> for Value {
impl FromGlibPtrNone<JSValueRef> for ValueRef {
unsafe fn from_glib_none(ptr: JSValueRef) -> Self {
Value {
ValueRef {
raw: ptr,
}
}
}

impl FromGlibPtrFull<JSValueRef> for Value {
impl FromGlibPtrFull<JSValueRef> for ValueRef {
unsafe fn from_glib_full(ptr: JSValueRef) -> Self {
Value {
ValueRef {
raw: ptr,
}
}
}

impl FromGlibPtrNone<JSGlobalContextRef> for GlobalContext {
impl FromGlibPtrNone<*mut JSCValue> for Value {
unsafe fn from_glib_none(ptr: *mut JSCValue) -> Self {
assert!(ptr != ptr::null_mut());
Value {
raw: *ptr,
}
}
}

impl FromGlibPtrNone<JSGlobalContextRef> for GlobalContextRef {
unsafe fn from_glib_none(ptr: JSGlobalContextRef) -> Self {
GlobalContext {
GlobalContextRef {
raw: ptr,
}
}
}

impl FromGlibPtrFull<JSGlobalContextRef> for GlobalContext {
impl FromGlibPtrFull<JSGlobalContextRef> for GlobalContextRef {
unsafe fn from_glib_full(ptr: JSGlobalContextRef) -> Self {
GlobalContext {
GlobalContextRef {
raw: ptr,
}
}
Expand Down

0 comments on commit 973e7ba

Please sign in to comment.