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
Comments
after sleeping and rereading the code I realized my tests had a typo and the code I wrote would have been correct, please disregard |
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)
}
} |
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
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
The text was updated successfully, but these errors were encountered: