Skip to main content

pxs_PixelObject

Struct pxs_PixelObject 

Source
pub struct pxs_PixelObject {
    pub type_name: String,
    pub ptr: *mut c_void,
    pub lang_ptr: Mutex<*mut c_void>,
    pub free_lang_ptr: Mutex<bool>,
    pub free_method: FreeMethod,
    pub callbacks: Vec<ModuleCallback>,
}
Expand description

A PixelScript Object.

The way this works is via the host, a Pseudo type can be created. So when the scripting language interacts with the object, it calls it’s pseudo methods.

example:

struct Person {
    const char* name;
    int age;

    Person(const char* name, int age) {
        this->name = name;
        this->age = age;
    }

    void set_name(const char* name) {
        this->name = name;
    }

    void set_age(int age) {
        this->age = age;
    }

    int get_age() {
        return this->age;
    }

    const char* get_name() {
        return this->name;
    }
};

void free_person(void* p) {
    // TODO
}
Var* person_set_name(int argc, Var** argv, void* opaque) {
    Var* object = argv[0];
    Person* p = object.value.object_val as Person;
    Var* name = argv[1];
    p->set_name(name.value.string_val);
    return NULL;
}
Var* new_person(int argc, Var** argv, void* opaque) {
    Person* p = malloc();
    PixelObject* object_ptr = pixelscript_new_object(p, free_person);
    pixelscript_object_add_callback(object_ptr, "set_name", person_set_name);
    return pixelscript_var_object(object_ptr);
}

// OOP base
pixelscript_add_object("Person", new_person);

// Or functional
pixelscript_add_callback("new_person", new_person);

In a JS example:

let p = new Person("Jordan");
p.set_name("James");

This is why a Objects are more like Pseudo types than actual class/objects.

Fields§

§type_name: String

Type name (this is a hash)

§ptr: *mut c_void

The Host pointer

§lang_ptr: Mutex<*mut c_void>

The language object pointer.

§free_lang_ptr: Mutex<bool>

Should the lang_ptr be freed by PixelScript?

§free_method: FreeMethod

The Method for freeing

§callbacks: Vec<ModuleCallback>

Callbacks with names.

Important to note that a PixelObject can not have static callbacks.

The first Var will always be the ptr.

Implementations§

Source§

impl pxs_PixelObject

Source

pub fn new(ptr: *mut c_void, free_method: FreeMethod, type_name: &str) -> Self

Source

pub fn add_callback(&mut self, name: &str, full_name: &str, idx: i32)

Source

pub fn update_lang_ptr(&self, n_ptr: *mut c_void)

Source

pub fn update_free_lang_ptr(&self, val: bool)

Trait Implementations§

Source§

impl Drop for pxs_PixelObject

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PtrMagic for pxs_PixelObject

Source§

fn into_raw(self) -> *mut Self

Moves the object to the heap and returns a raw pointer. Caller owns this memory but don’t worry about freeing it. The library frees it somewhere.
Source§

fn from_raw(ptr: *mut Self) -> Self

Safety: Only call this on a pointer created via into_raw.
Source§

unsafe fn from_borrow<'a>(ptr: *mut Self) -> &'a mut Self

Build from a Ptr but only get a reference, this means that the caller will still own the memory
Source§

impl Send for pxs_PixelObject

Source§

impl Sync for pxs_PixelObject

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> MaybeSend for T
where T: Send,