0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-13 16:33:53 +01:00

ircd::js: Improve trap callback arguments; add this value.

This commit is contained in:
Jason Volk 2016-11-03 16:48:01 -07:00
parent 292b3fc8e8
commit b8f5366f52
4 changed files with 18 additions and 15 deletions

View file

@ -36,15 +36,15 @@ class trap
std::map<persist_string, trap *, persist_string::less> children; std::map<persist_string, trap *, persist_string::less> children;
// Override these to define JS objects in C // Override these to define JS objects in C
virtual value on_call(object::handle, const args &); virtual value on_call(object::handle, object::handle, const args &);
virtual value on_set(object::handle, id::handle, value::handle); virtual value on_set(object::handle, id::handle, value::handle);
virtual value on_get(object::handle, id::handle, value::handle); virtual value on_get(object::handle, id::handle, value::handle);
virtual void on_add(object::handle, id::handle, value::handle); virtual void on_add(object::handle, id::handle, value::handle);
virtual bool on_del(object::handle, id::handle); virtual bool on_del(object::handle, id::handle);
virtual bool on_has(object::handle, id::handle); virtual bool on_has(object::handle, id::handle);
virtual void on_enu(object::handle); virtual void on_enu(object::handle);
virtual void on_ctor(object &, const args &); virtual void on_new(object::handle, object &, const args &);
virtual void on_dtor(JSObject &); virtual void on_gc(JSObject &);
private: private:
void host_exception(const char *fmt, ...) const AFP(2, 3); void host_exception(const char *fmt, ...) const AFP(2, 3);

View file

@ -32,7 +32,7 @@ struct trap_function
uint flags; uint flags;
protected: protected:
virtual value on_call(object::handle, const args &); virtual value on_call(object::handle callee, value::handle that, const args &);
private: private:
static trap_function &from(JSObject *const &); static trap_function &from(JSObject *const &);

View file

@ -427,14 +427,14 @@ noexcept try
assert(&our(c) == cx); assert(&our(c) == cx);
const struct args args(argc, argv); const struct args args(argc, argv);
//const auto that(args.computeThis(c)); const value that(args.computeThis(c));
const object func(args.callee()); const object func(args.callee());
auto &trap(from(func)); auto &trap(from(func));
log.debug("trap_function(%p) \"%s\": call", log.debug("trap_function(%p) \"%s\": call",
(const void *)&trap, (const void *)&trap,
trap.name.c_str()); trap.name.c_str());
args.rval().set(trap.on_call(func, args)); args.rval().set(trap.on_call(func, that, args));
return true; return true;
} }
catch(const jserror &e) catch(const jserror &e)
@ -469,6 +469,7 @@ ircd::js::trap_function::from(JSObject *const &func)
ircd::js::value ircd::js::value
ircd::js::trap_function::on_call(object::handle, ircd::js::trap_function::on_call(object::handle,
value::handle,
const args &) const args &)
{ {
return {}; return {};
@ -676,7 +677,7 @@ noexcept try
auto &trap(from(*obj)); auto &trap(from(*obj));
trap.debug("dtor %p", (const void *)obj); trap.debug("dtor %p", (const void *)obj);
trap.on_dtor(*obj); trap.on_gc(*obj);
} }
catch(const jserror &e) catch(const jserror &e)
{ {
@ -706,7 +707,7 @@ noexcept try
trap.debug("ctor: '%s'", trap.name().c_str()); trap.debug("ctor: '%s'", trap.name().c_str());
object ret(JS_NewObjectWithGivenProto(*cx, &trap.jsclass(), that)); object ret(JS_NewObjectWithGivenProto(*cx, &trap.jsclass(), that));
trap.on_ctor(ret, args); trap.on_new(that, ret, args);
args.rval().set(ret); args.rval().set(ret);
return true; return true;
} }
@ -742,7 +743,7 @@ noexcept try
trap_that.debug("call: '%s'", trap_func.name().c_str()); trap_that.debug("call: '%s'", trap_func.name().c_str());
trap_func.debug("call"); trap_func.debug("call");
args.rval().set(trap_func.on_call(func, args)); args.rval().set(trap_func.on_call(func, that, args));
return true; return true;
} }
catch(const jserror &e) catch(const jserror &e)
@ -1048,13 +1049,14 @@ const
} }
void void
ircd::js::trap::on_dtor(JSObject &) ircd::js::trap::on_gc(JSObject &)
{ {
} }
void void
ircd::js::trap::on_ctor(object &obj, ircd::js::trap::on_new(object::handle,
const args &) object &,
const args &)
{ {
} }
@ -1067,7 +1069,7 @@ bool
ircd::js::trap::on_has(object::handle, ircd::js::trap::on_has(object::handle,
id::handle) id::handle)
{ {
return true; return false;
} }
bool bool
@ -1108,6 +1110,7 @@ ircd::js::trap::on_set(object::handle,
ircd::js::value ircd::js::value
ircd::js::trap::on_call(object::handle, ircd::js::trap::on_call(object::handle,
object::handle,
const args &) const args &)
{ {
return {}; return {};

View file

@ -29,7 +29,7 @@ struct future
{ {
uint64_t id_ctr; uint64_t id_ctr;
void on_ctor(object &, const args &args) override; void on_new(object::handle, object &, const args &args) override;
future(); future();
} }
@ -46,7 +46,7 @@ future::future()
} }
void void
future::on_ctor(object &obj, const args &args) future::on_new(object::handle, object &obj, const args &args)
{ {
auto &task(task::get()); auto &task(task::get());