Thread overview
need help to find a way to replace c macro
Oct 15
Dakota
Oct 15
monkyyy
Oct 15
Dakota
Oct 15
ryuukk_
5 days ago
Dakota
October 15
typedef union JSCFunctionType {
    JSCFunction *generic;
    JSValue (*generic_magic)(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
    JSCFunction *constructor;
    JSValue (*constructor_magic)(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic);
    JSCFunction *constructor_or_func;
    double (*f_f)(double);
    double (*f_f_f)(double, double);
    JSValue (*getter)(JSContext *ctx, JSValueConst this_val);
    JSValue (*setter)(JSContext *ctx, JSValueConst this_val, JSValueConst val);
    JSValue (*getter_magic)(JSContext *ctx, JSValueConst this_val, int magic);
    JSValue (*setter_magic)(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic);
    JSValue (*iterator_next)(JSContext *ctx, JSValueConst this_val,
                             int argc, JSValueConst *argv, int *pdone, int magic);
} JSCFunctionType;

typedef struct JSCFunctionListEntry {
    const char *name;
    uint8_t prop_flags;
    uint8_t def_type;
    int16_t magic;
    union {
        struct {
            uint8_t length; /* XXX: should move outside union */
            uint8_t cproto; /* XXX: should move outside union */
            JSCFunctionType cfunc;
        } func;
        struct {
            JSCFunctionType get;
            JSCFunctionType set;
        } getset;
        struct {
            const char *name;
            int base;
        } alias;
        struct {
            const struct JSCFunctionListEntry *tab;
            int len;
        } prop_list;
        const char *str;
        int32_t i32;
        int64_t i64;
        double f64;
    } u;
} JSCFunctionListEntry;

#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, .u = { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }



static const JSCFunctionListEntry js_std_funcs[] = {
    JS_CFUNC_DEF("exit", 1, js_std_exit ),
    JS_CFUNC_DEF("gc", 0, js_std_gc ),
    JS_CFUNC_DEF("evalScript", 1, js_evalScript ),
    JS_CFUNC_DEF("loadScript", 1, js_loadScript ),
    JS_CFUNC_DEF("getenv", 1, js_std_getenv ),
    JS_CFUNC_DEF("setenv", 1, js_std_setenv ),
    JS_CFUNC_DEF("unsetenv", 1, js_std_unsetenv ),
    JS_CFUNC_DEF("getenviron", 1, js_std_getenviron ),
    JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ),
    JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
    JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
    JS_CFUNC_DEF("parseExtJSON", 1, js_std_parseExtJSON ),

    /* FILE I/O */
    JS_CFUNC_DEF("open", 2, js_std_open ),
    JS_CFUNC_DEF("popen", 2, js_std_popen ),
    JS_CFUNC_DEF("fdopen", 2, js_std_fdopen ),
    JS_CFUNC_DEF("tmpfile", 0, js_std_tmpfile ),
    JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 0 ),
    JS_CFUNC_DEF("printf", 1, js_std_printf ),
    JS_CFUNC_DEF("sprintf", 1, js_std_sprintf ),
};

the js_std_funcs better to be defined as const, so in runtime the memory address is non-writable for some platform.

I am not sure how to translate JS_CFUNC_DEF, JS_CFUNC_MAGIC_DEF into D, so I can use it like c here to create a nice const array of JSCFunctionListEntry

October 15

On Tuesday, 15 October 2024 at 12:05:16 UTC, Dakota wrote:

>
typedef union JSCFunctionType {
    JSCFunction *generic;
    JSValue (*generic_magic)(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
    JSCFunction *constructor;
    JSValue (*constructor_magic)(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic);
    JSCFunction *constructor_or_func;
    double (*f_f)(double);
    double (*f_f_f)(double, double);
    JSValue (*getter)(JSContext *ctx, JSValueConst this_val);
    JSValue (*setter)(JSContext *ctx, JSValueConst this_val, JSValueConst val);
    JSValue (*getter_magic)(JSContext *ctx, JSValueConst this_val, int magic);
    JSValue (*setter_magic)(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic);
    JSValue (*iterator_next)(JSContext *ctx, JSValueConst this_val,
                             int argc, JSValueConst *argv, int *pdone, int magic);
} JSCFunctionType;

typedef struct JSCFunctionListEntry {
    const char *name;
    uint8_t prop_flags;
    uint8_t def_type;
    int16_t magic;
    union {
        struct {
            uint8_t length; /* XXX: should move outside union */
            uint8_t cproto; /* XXX: should move outside union */
            JSCFunctionType cfunc;
        } func;
        struct {
            JSCFunctionType get;
            JSCFunctionType set;
        } getset;
        struct {
            const char *name;
            int base;
        } alias;
        struct {
            const struct JSCFunctionListEntry *tab;
            int len;
        } prop_list;
        const char *str;
        int32_t i32;
        int64_t i64;
        double f64;
    } u;
} JSCFunctionListEntry;

#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, .u = { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }



static const JSCFunctionListEntry js_std_funcs[] = {
    JS_CFUNC_DEF("exit", 1, js_std_exit ),
    JS_CFUNC_DEF("gc", 0, js_std_gc ),
    JS_CFUNC_DEF("evalScript", 1, js_evalScript ),
    JS_CFUNC_DEF("loadScript", 1, js_loadScript ),
    JS_CFUNC_DEF("getenv", 1, js_std_getenv ),
    JS_CFUNC_DEF("setenv", 1, js_std_setenv ),
    JS_CFUNC_DEF("unsetenv", 1, js_std_unsetenv ),
    JS_CFUNC_DEF("getenviron", 1, js_std_getenviron ),
    JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ),
    JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
    JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
    JS_CFUNC_DEF("parseExtJSON", 1, js_std_parseExtJSON ),

    /* FILE I/O */
    JS_CFUNC_DEF("open", 2, js_std_open ),
    JS_CFUNC_DEF("popen", 2, js_std_popen ),
    JS_CFUNC_DEF("fdopen", 2, js_std_fdopen ),
    JS_CFUNC_DEF("tmpfile", 0, js_std_tmpfile ),
    JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 0 ),
    JS_CFUNC_DEF("printf", 1, js_std_printf ),
    JS_CFUNC_DEF("sprintf", 1, js_std_sprintf ),
};

the js_std_funcs better to be defined as const, so in runtime the memory address is non-writable for some platform.

I am not sure how to translate JS_CFUNC_DEF, JS_CFUNC_MAGIC_DEF into D, so I can use it like c here to create a nice const array of JSCFunctionListEntry

Your probably going to need to simplify that block of code looks awful

October 15

On Tuesday, 15 October 2024 at 12:13:36 UTC, monkyyy wrote:

>

Your probably going to need to simplify that block of code looks awful

source code here: https://github.com/bellard/quickjs/blob/master/quickjs.h#L1045

October 15

Try this great tool by denis korpel https://dkorpel.github.io/ctod/

I've been using it every so often to port C stuff, it's been very helpful

@nogc nothrow:
extern(C): __gshared:
union JSCFunctionType {
    JSCFunction* generic;
    JSValue function(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic) generic_magic;
    JSCFunction* constructor;
    JSValue function(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv, int magic) constructor_magic;
    JSCFunction* constructor_or_func;
    double function(double) f_f;
    double function(double, double) f_f_f;
    JSValue function(JSContext* ctx, JSValueConst this_val) getter;
    JSValue function(JSContext* ctx, JSValueConst this_val, JSValueConst val) setter;
    JSValue function(JSContext* ctx, JSValueConst this_val, int magic) getter_magic;
    JSValue function(JSContext* ctx, JSValueConst this_val, JSValueConst val, int magic) setter_magic;
    JSValue function(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int* pdone, int magic) iterator_next;
}

struct JSCFunctionListEntry {
    const(char)* name;
    ubyte prop_flags;
    ubyte def_type;
    short magic;
    union _U {
        struct _Func {
            ubyte length; /* XXX: should move outside union */
            ubyte cproto; /* XXX: should move outside union */
            JSCFunctionType cfunc;
        }_Func func;
        struct _Getset {
            JSCFunctionType get;
            JSCFunctionType set;
        }_Getset getset;
        struct _Alias_ {
            const(char)* name;
            int base;
        }_Alias_ alias_;
        struct _Prop_list {
            const(JSCFunctionListEntry)* tab;
            int len;
        }_Prop_list prop_list;
        const(char)* str;
        int i32;
        long i64;
        double f64;
    }_U u;
}

enum string JS_CFUNC_DEF(string name, string length, string func1) = `{ ` ~ name ~ `, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { ` ~ length ~ `, JS_CFUNC_generic, { .generic = ` ~ func1 ~ ` } `;
enum string JS_CFUNC_MAGIC_DEF(string name, string length, string func1, string magic) = `{ ` ~ name ~ `, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, ` ~ magic ~ `, .u = { .func = { ` ~ length ~ `, JS_CFUNC_generic_magic, { .generic_magic = ` ~ func1 ~ ` } `;
enum string JS_CFUNC_SPECIAL_DEF(string name, string length, string cproto, string func1) = `{ name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }`;
enum string JS_ITERATOR_NEXT_DEF(string name, string length, string func1, string magic) = `{ ` ~ name ~ `, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, ` ~ magic ~ `, .u = { .func = { ` ~ length ~ `, JS_CFUNC_iterator_next, { .iterator_next = ` ~ func1 ~ ` } `;
enum string JS_CGETSET_DEF(string name, string fgetter, string fsetter) = `{ ` ~ name ~ `, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = ` ~ fgetter ~ ` }, .set = { .setter = ` ~ fsetter ~ ` } `;
enum string JS_CGETSET_MAGIC_DEF(string name, string fgetter, string fsetter, string magic) = `{ ` ~ name ~ `, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, ` ~ magic ~ `, .u = { .getset = { .get = { .getter_magic = ` ~ fgetter ~ ` }, .set = { .setter_magic = ` ~ fsetter ~ ` } `;



private const(JSCFunctionListEntry)[20] js_std_funcs = [
    mixin(JS_CFUNC_DEF!(`"exit"`, `1`, `js_std_exit` )),
    mixin(JS_CFUNC_DEF!(`"gc"`, `0`, `js_std_gc` )),
    mixin(JS_CFUNC_DEF!(`"evalScript"`, `1`, `js_evalScript` )),
    mixin(JS_CFUNC_DEF!(`"loadScript"`, `1`, `js_loadScript` )),
    mixin(JS_CFUNC_DEF!(`"getenv"`, `1`, `js_std_getenv` )),
    mixin(JS_CFUNC_DEF!(`"setenv"`, `1`, `js_std_setenv` )),
    mixin(JS_CFUNC_DEF!(`"unsetenv"`, `1`, `js_std_unsetenv` )),
    mixin(JS_CFUNC_DEF!(`"getenviron"`, `1`, `js_std_getenviron` )),
    mixin(JS_CFUNC_DEF!(`"urlGet"`, `1`, `js_std_urlGet` )),
    mixin(JS_CFUNC_DEF!(`"loadFile"`, `1`, `js_std_loadFile` )),
    mixin(JS_CFUNC_DEF!(`"strerror"`, `1`, `js_std_strerror` )),
    mixin(JS_CFUNC_DEF!(`"parseExtJSON"`, `1`, `js_std_parseExtJSON` )),

    /* FILE I/O */
    mixin(JS_CFUNC_DEF!(`"open"`, `2`, `js_std_open` )),
    mixin(JS_CFUNC_DEF!(`"popen"`, `2`, `js_std_popen` )),
    mixin(JS_CFUNC_DEF!(`"fdopen"`, `2`, `js_std_fdopen` )),
    mixin(JS_CFUNC_DEF!(`"tmpfile"`, `0`, `js_std_tmpfile` )),
    mixin(JS_CFUNC_MAGIC_DEF!(`"puts"`, `1`, `js_std_file_puts`, `0` )),
    mixin(JS_CFUNC_DEF!(`"printf"`, `1`, `js_std_printf` )),
    mixin(JS_CFUNC_DEF!(`"sprintf"`, `1`, `js_std_sprintf` )),
];
5 days ago

On Tuesday, 15 October 2024 at 15:29:09 UTC, ryuukk_ wrote:

>

[...]

Thanks very much.