Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.log in modules #97

Closed
Gentle opened this issue Nov 11, 2022 · 3 comments
Closed

console.log in modules #97

Gentle opened this issue Nov 11, 2022 · 3 comments

Comments

@Gentle
Copy link

Gentle commented Nov 11, 2022

I am currently inserting a console object into globals of my context to get console.log functionality working, but how do I get that global into a module? If I ctx.compile() my module, the exported functions don't see console.log and I don't see a way to modify globals of a module

@Gentle
Copy link
Author

Gentle commented Nov 11, 2022

after sleeping and rereading the code I realized my tests had a typo and the code I wrote would have been correct, please disregard

@Gentle Gentle closed this as completed Nov 11, 2022
@chertov
Copy link

chertov commented Dec 8, 2022

my simplest example, maybe it will be useful to someone

use rquickjs::{Class, ClassDef, ClassId, Ctx, FromJs, IntoJs, Method, Object, Result, Value};

pub fn init<'js>(id: String, ctx: Ctx) -> Result<()> {
    Class::<Console>::register(ctx)?;
    ctx.globals().set("console", Console { id }.into_js(ctx)?)?;
    Ok(())
}

struct Console {
    id: String,
}
impl Console {
    fn log(&self, str: String) {
        trace!("[vm:{}] {str}", self.id);
    }
}
impl ClassDef for Console {
    const CLASS_NAME: &'static str = "Console";
    unsafe fn class_id() -> &'static mut ClassId {
        static mut CLASS_ID: ClassId = ClassId::new();
        &mut CLASS_ID
    }
    const HAS_PROTO: bool = true;
    fn init_proto<'js>(ctx: Ctx<'js>, proto: &Object<'js>) -> Result<()> {
        proto.set("log", rquickjs::Func::from(Method(Self::log)))?;
        Ok(())
    }
}

impl<'js> IntoJs<'js> for Console {
    fn into_js(self, ctx: Ctx<'js>) -> Result<Value<'js>> {
        self.into_js_obj(ctx)
    }
}
impl<'js> FromJs<'js> for &'js Console {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        Console::from_js_ref(ctx, value)
    }
}

@Sytten
Copy link
Contributor

Sytten commented Jun 12, 2023

Updated version of the above:

use rquickjs::class::ClassDef;
use rquickjs::function::{Func, Method};
use rquickjs::{Class, ClassId, Ctx, FromJs, IntoJs, Object, Result, Value};

pub fn init<'js>(id: String, ctx: Ctx) -> Result<()> {
    Class::<Console>::register(ctx)?;
    ctx.globals().set("console", Console { id }.into_js(ctx)?)?;
    Ok(())
}

struct Console {
    id: String,
}
impl Console {
    fn log(&self, str: String) {
        println!("[vm:{}] {str}", self.id);
    }
}
impl ClassDef for Console {
    const CLASS_NAME: &'static str = "Console";
    fn class_id() -> &'static ClassId {
        static CLASS_ID: ClassId = ClassId::new();
        &CLASS_ID
    }
    const HAS_PROTO: bool = true;
    fn init_proto<'js>(_ctx: Ctx<'js>, proto: &Object<'js>) -> Result<()> {
        proto.set("log", Func::from(Method(Self::log)))?;
        Ok(())
    }
}

impl<'js> IntoJs<'js> for Console {
    fn into_js(self, ctx: Ctx<'js>) -> Result<Value<'js>> {
        self.into_js_obj(ctx)
    }
}
impl<'js> FromJs<'js> for &'js Console {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        Console::from_js_ref(ctx, value)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants