RPM Package Manager, CVS Repository http://rpm5.org/cvs/ ____________________________________________________________________________
Server: rpm5.org Name: Jeff Johnson Root: /v/rpm/cvs Email: j...@rpm5.org Module: rpm Date: 12-Jul-2017 21:48:42 Branch: rpm-5_4 Handle: 2017071219484100 Modified files: (Branch: rpm-5_4) rpm/js rpmbf-js.c rpmbf-js.h Log: rpmbf-js: proof-of-concept rpmbfClass. Summary: Revision Changes Path 1.10.4.11 +230 -189 rpm/js/rpmbf-js.c 1.4.4.1 +1 -1 rpm/js/rpmbf-js.h ____________________________________________________________________________ patch -p0 <<'@@ .' Index: rpm/js/rpmbf-js.c ============================================================================ $ cvs diff -u -r1.10.4.10 -r1.10.4.11 rpmbf-js.c --- rpm/js/rpmbf-js.c 11 Jul 2017 11:50:41 -0000 1.10.4.10 +++ rpm/js/rpmbf-js.c 12 Jul 2017 19:48:41 -0000 1.10.4.11 @@ -13,140 +13,121 @@ #include "debug.h" /*@unchecked@*/ -static int _debug = 0; - -#define rpmbf_addprop JS_PropertyStub -#define rpmbf_delprop JS_PropertyStub -#define rpmbf_convert JS_ConvertStub - +static int _debug = -1; +#if !defined(SPEW) +# define SPEW(_fmt, ...) \ + if (_debug || _rpmbf_debug) \ + fprintf(stderr, _fmt, __VA_ARGS__) +#endif + static bool rpmbf_add(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf bf = (rpmbf) ptr; - bool ok = false; - const char * _s = NULL; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); - args.rval().setBoolean(false); - if (!(ok = JS_ConvertArguments(cx, args, "s", &_s))) - goto exit; + RootedString str(cx, ToString(cx, args[0])); + const char * _s = JS_EncodeStringToUTF8(cx, str); args.rval().setBoolean(rpmbfAdd(bf, _s, 0) == 0); - ok = true; -exit: return ok; } static bool rpmbf_chk(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf bf = (rpmbf) ptr; - bool ok = false; - const char * _s = NULL; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); - args.rval().setBoolean(false); - if (!(ok = JS_ConvertArguments(cx, args, "s", &_s))) - goto exit; + RootedString str(cx, ToString(cx, args[0])); + const char * _s = JS_EncodeStringToUTF8(cx, str); args.rval().setBoolean(rpmbfChk(bf, _s, 0) > 0); - ok = true; -exit: return ok; } static bool rpmbf_clr(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); + CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf bf = (rpmbf) ptr; - bool ok = false; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); args.rval().setBoolean(rpmbfClr(bf) == 0); - ok = true; return ok; } static bool rpmbf_del(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf bf = (rpmbf) ptr; - bool ok = false; - const char * _s = NULL; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); - args.rval().setBoolean(false); - if (!(ok = JS_ConvertArguments(cx, args, "s", &_s))) - goto exit; + RootedString str(cx, ToString(cx, args[0])); + const char * _s = JS_EncodeStringToUTF8(cx, str); args.rval().setBoolean(rpmbfDel(bf, _s, 0) == 0); - ok = true; -exit: return ok; } static bool rpmbf_intersect(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf _a = (rpmbf) ptr; - JSObject * o = NULL; - rpmbf _b = NULL; - bool ok = false; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); - args.rval().setBoolean(false); - if (!(ok = JS_ConvertArguments(cx, args, "o", &o)) - || (_b = JS_GetInstancePrivate(cx, o, &rpmbfClass, NULL)) == NULL) - goto exit; - - if (!rpmbfIntersect(_a, _b)) - args.rval().setBoolean(true); - ok = true; -exit: + RootedObject o(cx, ToObject(cx, args[0])); + rpmbf _b = (rpmbf) JS_GetInstancePrivate(cx, o, &rpmbfClass, nullptr); + + args.rval().setBoolean(rpmbfIntersect(_a, _b) == 0); return ok; } static bool rpmbf_union(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); CallArgs args = CallArgsFromVp(argc, vp); RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); rpmbf _a = (rpmbf) ptr; - JSObject * o = NULL; - rpmbf _b = NULL; - bool ok = false; + bool ok = true; _METHOD_DEBUG_ENTRY(_debug); - args.rval().setBoolean(false); - if (!(ok = JS_ConvertArguments(cx, args, "o", &o)) - || (_b = JS_GetInstancePrivate(cx, o, &rpmbfClass, NULL)) == NULL) - goto exit; - - if (!rpmbfUnion(_a, _b)) - args.rval().setBoolean(true); - ok = true; -exit: + RootedObject o(cx, ToObject(cx, args[0])); + rpmbf _b = (rpmbf) JS_GetInstancePrivate(cx, o, &rpmbfClass, nullptr); + + args.rval().setBoolean(rpmbfUnion(_a, _b) == 0); return ok; } @@ -161,106 +142,99 @@ }; /* --- Object properties */ -enum rpmbf_tinyid { - _DEBUG = -2, - _LENGTH = -3, -}; +static bool +rpmbf_getprop(JSContext *cx, unsigned argc, Value* vp) +{ +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); + CallArgs args = CallArgsFromVp(argc, vp); -static JSPropertySpec rpmbf_props[] = { - {"debug", _DEBUG, JSPROP_ENUMERATE, NULL, NULL}, - {NULL, 0, 0, NULL, NULL} -}; + args.rval().setInt32(_debug); + return true; +} static bool -rpmbf_getprop(JSContext *cx, JSObject *obj, jsid id, jsval *vp) +rpmbf_setprop(JSContext *cx, unsigned argc, Value* vp) { - void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); - jsint tiny = JSVAL_TO_INT(id); +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); + CallArgs args = CallArgsFromVp(argc, vp); -#ifdef NOTYET - rpmbf bf = (rpmbf) ptr; -_PROP_DEBUG_ENTRY(_debug < 0); -#endif + args.rval().setInt32(_debug); + if (!ToInt32(cx, args[0], &_debug)) + return false; + return true; +} - /* XXX the class has ptr == NULL, instances have ptr != NULL. */ - if (ptr == NULL) - return true; +static JSPropertySpec rpmbf_props[] = { + JS_PSGS("debug", rpmbf_getprop, rpmbf_setprop, JSPROP_ENUMERATE), + JS_PS_END +}; - switch (tiny) { - case _DEBUG: - *vp = INT_TO_JSVAL(_debug); - break; - default: #ifdef NOTYET - if (JSVAL_IS_STRING(id) && *vp == JSVAL_VOID) { - const char * name = JS_EncodeString(cx, JS_ValueToString(cx, id)); - const char * _path = rpmGetPath(_defvar, "/", name, NULL); - const char * _value = NULL; - if (rpmbfGet(bf, _path, &_value) >= 0 && _value != NULL) - *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, _value)); - _value = _free(_value); - _path = _free(_path); - name = _free(name); - break; - } -#endif - break; - } - +static bool +rpmbf_addProperty(JSContext *cx, + Handle<JSObject*> obj, + Handle<jsid> id, + Handle<Value> vp) +{ return true; } +#else +#define rpmbf_addProperty nullptr +#endif +#ifdef NOTYET static bool -rpmbf_setprop(JSContext *cx, JSObject *obj, jsid id, bool strict, jsval *vp) +rpmbf_delProperty(JSContext *cx, + Handle<JSObject*> obj, + Handle<jsid> id, + ObjectOpResult& result) { - void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); - jsint tiny = JSVAL_TO_INT(id); - -#ifdef NOTYET - rpmbf bf = (rpmbf) ptr; -_PROP_DEBUG_ENTRY(_debug < 0); + return true; +} +#else +#define rpmbf_delProperty nullptr #endif - /* XXX the class has ptr == NULL, instances have ptr != NULL. */ - if (ptr == NULL) - return true; - - switch (tiny) { - case _DEBUG: - if (!JS_ValueToInt32(cx, *vp, &_debug)) - break; - break; - default: #ifdef NOTYET - /* XXX expr = undefined same as deleting? */ - if (JSVAL_IS_STRING(id)) { - const char * name = JS_EncodeString(cx, JS_ValueToString(cx, id)); - const char * expr = JS_EncodeString(cx, JS_ValueToString(cx, *vp)); - /* XXX should *setprop be permitted to delete NAME?!? */ - /* XXX return is no. nodes in EXPR match. */ - (void) rpmbfDefvar(bf, name, expr); - expr = _free(expr); - name = _free(name); - break; - } +static bool +rpmbf_getProperty(JSContext *cx, + Handle<JSObject*> obj, + Handle<jsid> id, + MutableHandle<Value> vp) +{ + return true; +} +#else +#define rpmbf_getProperty nullptr #endif - break; - } +#ifdef NOTYET +static bool +rpmbf_setProperty(JSContext *cx, + Handle<JSObject*> obj, + Handle<jsid> id, + MutableHandle<Value> vp, + ObjectOpResult& result) +{ return true; } +#else +#define rpmbf_setProperty nullptr +#endif +#ifdef NOTYET static bool -rpmbf_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags, - JSObject **objp) +rpmbf_resolve(JSContext *cx, + Handle<JSObject*> obj, + Handle<jsid> id, + bool *result) { +// _RESOLVE_DEBUG_ENTRY(_debug); + +#ifdef DYING void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); -#ifdef NOTYET rpmbf bf = (rpmbf) ptr; -_RESOLVE_DEBUG_ENTRY(_debug); -#endif - if (ptr == NULL) { /* don't resolve to parent prototypes objects. */ *objp = NULL; goto exit; @@ -316,16 +290,32 @@ *objp = obj; /* XXX always resolve in this object. */ exit: +#endif /* DYING */ + return true; } +#else +#define rpmbf_resolve nullptr +#endif +#ifdef NOTYET static bool -rpmbf_enumerate(JSContext *cx, JSObject *obj, JSIterateOp op, - jsval *statep, jsid *idp) +rpmbf_mayResolve(const JSAtomState& state, jsid id, JSObject *obj) { + return true; +} +#else +#define rpmbf_mayResolve nullptr +#endif -_ENUMERATE_DEBUG_ENTRY(_debug); +#ifdef NOTYET +static bool +rpmbf_enumerate(JSContext *cx, JS::Handle<JSObject*> obj) +{ +SPEW("==> %s(%p,%p)\n", __FUNCTION__, cx, obj); +// _ENUMERATE_DEBUG_ENTRY(_debug); +#ifdef DYING switch (op) { case JSENUMERATE_INIT: case JSENUMERATE_INIT_ALL: @@ -342,107 +332,158 @@ *statep = JSVAL_NULL; break; } +#endif /* DYING */ + + return true; +} +#else +#define rpmbf_enumerate nullptr +#endif + +#ifdef NOTYET +static bool +rpmbf_call(JSContext *cx, unsigned argc, Value* vp) +{ +SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); + CallArgs args = CallArgsFromVp(argc, vp); + RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); + void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); + rpmbf _a = (rpmbf) ptr; + + return true; +} +#else +#define rpmbf_call nullptr +#endif + +#ifdef NOTYET +static bool +rpmbf_hasInstance(JSContext *cx, + Handle<JSObject*> obj, + MutableHandle<Value>, + bool *result) +{ return true; } +#else +#define rpmbf_hasInstance nullptr +#endif + +#ifdef NOTYET +static void +rpmbf_trace(JSTracer *foo, JSObject *obj) +{ +} +#else +#define rpmbf_trace nullptr +#endif /* --- Object ctors/dtors */ static rpmbf -rpmbf_init(JSContext *cx, JSObject *obj, size_t _m, size_t _k, - unsigned int _flags) +rpmbf_init(JSObject *obj, size_t _m, size_t _k, unsigned int _flags) { - rpmbf bf; - - if ((bf = rpmbfNew(_m, _k, _flags)) == NULL) - return NULL; - if (!JS_SetPrivate(cx, obj, bf)) { - /* XXX error msg */ - (void) rpmbfFree(bf); - return NULL; - } +//SPEW("==> %s(%p, %zu, %zu, %u)\n", __FUNCTION__, obj, _m, _k, _flags); +_rpmbf_debug = _debug; + rpmbf bf = rpmbfNew(_m, _k, _flags); + if (bf) + JS_SetPrivate(obj, bf); return bf; } +#ifndef NOTYET static void -rpmbf_dtor(JSContext *cx, JSObject *obj) +rpmbf_dtor(JSFreeOp* fop, JSObject *obj) { - void * ptr = JS_GetInstancePrivate(cx, obj, &rpmbfClass, nullptr); +//SPEW("==> %s(%p, %p)\n", __FUNCTION__, fop, obj); + void * ptr = JS_GetPrivate(obj); rpmbf bf = (rpmbf) ptr; - _DTOR_DEBUG_ENTRY(_debug); - bf = rpmbfFree(bf); } +#else +#define rpmbf_dtor nullptr +#endif static bool rpmbf_ctor(JSContext *cx, unsigned argc, Value* vp) { +//SPEW("==> %s(%p,%u,%p)\n", __FUNCTION__, cx, argc, vp); +_CTOR_DEBUG_ENTRY(_debug); CallArgs args = CallArgsFromVp(argc, vp); - JSObject *obj = JS_NewObjectForConstructor(cx, vp); + + RootedObject obj(cx, JS_NewObjectForConstructor(cx, &rpmbfClass, args)); + if (!obj + || !JS_FreezeObject(cx, obj)) + return false; + unsigned int _m = 0; unsigned int _k = 0; unsigned int _flags = 0; - bool ok = false; - -_CTOR_DEBUG_ENTRY(_debug); - if (!(ok = JS_ConvertArguments(cx, args, "/uuu", &_m, &_k, &_flags))) - goto exit; - ok = false; /* XXX reset */ - - if (JS_IsConstructing(cx, vp)) { - if ((obj = JS_NewObject(cx, &rpmbfClass, NULL, NULL)) == NULL) - goto exit; - if (rpmbf_init(cx, obj, _m, _k, _flags) == NULL) - goto exit; - } else { - obj = JS_THIS_OBJECT(cx, vp); - *vp = OBJECT_TO_JSVAL(obj); + switch (argc) { + default: + return false; + case 3: + if (!ToUint32(cx, args[2], &_flags)) + return false; + case 2: + if (!ToUint32(cx, args[1], &_k)) + return false; + case 1: + if (!ToUint32(cx, args[0], &_m)) + return false; + case 0: + break; } - ok = true; -exit: - return ok; + if (rpmbf_init(obj, _m, _k, _flags) == NULL) + return false; + + args.rval().setObject(*obj); + return true; } /* --- Class initialization */ JSClass rpmbfClass = { /* XXX class should be "Bloom" eventually, avoid name conflicts for now */ - "Bf", JSCLASS_NEW_RESOLVE | JSCLASS_NEW_ENUMERATE | JSCLASS_HAS_PRIVATE, - rpmbf_addprop, rpmbf_delprop, rpmbf_getprop, rpmbf_setprop, - (JSEnumerateOp)rpmbf_enumerate, (JSResolveOp)rpmbf_resolve, - rpmbf_convert, rpmbf_dtor, - JSCLASS_NO_OPTIONAL_MEMBERS + "Bf", + JSCLASS_HAS_PRIVATE, + rpmbf_addProperty, // addProperty + rpmbf_delProperty, // delProperty + rpmbf_getProperty, // getProperty + rpmbf_setProperty, // setProperty + rpmbf_enumerate, // enumerate + rpmbf_resolve, // resolve + rpmbf_mayResolve, // mayResolve, + rpmbf_dtor, // finalize + rpmbf_call, // call + rpmbf_hasInstance, // hasInstance + rpmbf_ctor, // construct + rpmbf_trace, // trace }; JSObject * -rpmjs_InitBfClass(JSContext *cx, JSObject* obj) +rpmjs_InitBfClass(JSContext *cx, HandleObject obj) { - JSObject * o; - -if (_debug) -fprintf(stderr, "==> %s(%p,%p)\n", __FUNCTION__, cx, obj); - - o = JS_InitClass(cx, obj, NULL, &rpmbfClass, rpmbf_ctor, 1, - rpmbf_props, rpmbf_funcs, NULL, NULL); -assert(o != NULL); +SPEW("==> %s(%p,%p)\n", __FUNCTION__, cx, obj.get()); + RootedObject o(cx); + o = JS_InitClass(cx, obj, NULL, + &rpmbfClass, rpmbf_ctor, 0, rpmbf_props, rpmbf_funcs, + NULL, NULL); return o; } JSObject * rpmjs_NewBfObject(JSContext *cx, size_t _m, size_t _k, unsigned int _flags) { - JSObject *obj; - rpmbf bf; - - if ((obj = JS_NewObject(cx, &rpmbfClass, NULL, NULL)) == NULL) { - /* XXX error msg */ +SPEW("==> %s(%p,%u,%u,%u)\n", __FUNCTION__, cx, _m, _k, _flags); + JSObject *obj = JS_NewObject(cx, &rpmbfClass); + if (!obj) return NULL; - } - if ((bf = rpmbf_init(cx, obj, _m, _k, _flags)) == NULL) { - /* XXX error msg */ + + rpmbf bf = rpmbf_init(obj, _m, _k, _flags); + if (!bf) return NULL; - } + return obj; } - -GPSEE_MODULE_WRAP(rpmbf, Bf, true) @@ . patch -p0 <<'@@ .' Index: rpm/js/rpmbf-js.h ============================================================================ $ cvs diff -u -r1.4 -r1.4.4.1 rpmbf-js.h --- rpm/js/rpmbf-js.h 10 Jan 2010 18:29:12 -0000 1.4 +++ rpm/js/rpmbf-js.h 12 Jul 2017 19:48:41 -0000 1.4.4.1 @@ -14,7 +14,7 @@ #endif extern JSObject * -rpmjs_InitBfClass(JSContext *cx, JSObject* obj); +rpmjs_InitBfClass(JSContext *cx, HandleObject obj); extern JSObject * rpmjs_NewBfObject(JSContext *cx, size_t _m, size_t _k, unsigned int _flags); @@ . ______________________________________________________________________ RPM Package Manager http://rpm5.org CVS Sources Repository rpm-cvs@rpm5.org