I'm struggling with a 'C' tp PB/DLL6 conversion for some extensions that I'm writing for the MacroMedia dreamweaver product.
The 'C' looks pretty simple, but the obvious conversion isn't the correct one!
The 'C' (stripped down a bit for readability) is:
'-----------------------------------------------------------
typedef long JSBool;
typedef JSBool (*JSNative)(JSContext *cx, JSObject *obj, unsigned int argc,
jsval *argv, jsval *rval);
/* JSBool JS_DefineFunction(char *name, JSNative call, unsigned int nargs) */
#define JS_DefineFunction(n, c, a) \
(mmEnv.defineFunction ? (*(mmEnv.defineFunction))(mmEnv.libObj, n, c, a) \
: JS_FALSE)
typedef struct {
JSObject *libObj;
JSBool (*defineFunction)(JSObject *libObj, char *name, JSNative call,
unsigned int nargs);
.
.
} MM_Environment;
extern MM_Environment mmEnv;
.
.
.
MM_Init()
{
JS_DefineFunction("computeSum", computeSum, 2);
}
JSBool
computeSum(JSContext *cx, JSObject *obj, unsigned int argc,
jsval *argv, jsval *rval)
{
.
.
.
return JS_TRUE;
}
'-----------------------------------------------------
O.k., so JSNative is simply a pointer to a function with 5 arguments - no problem.
I start to run into problem with the simple-looking line (above):
JS_DefineFunction("computeSum", computeSum, 2);
I realise that this function call refers to the previous typedef, so it actually has 4 parameters... a pointer to an object, a pointer to a string, a pointer to a function and an integer.
Now then, the problems begin. Dreamweaver passes us a pointer to the content of the MMenv structure and lets us copy it to a local structure. I assumed that to call the function, all I needed to do was to take the second DWORD in the structure as a pointer to the function, and to use CALL DWORD.
My code is:
Declare Function JS_DefineFunction Alias "JS_DefineFunction" (JSObject As Dword,FunctionName As Asciiz, JSNative As Dword, ByVal nargs As Integer) As Long
.
.
.
ComputeSumPointer=CodePtr(ComputeSum)
Call Dword FunctionAddress Using JS_DefineFunction
(MM_Environment.libobj,"ComputeSum", ComputeSumPointer, 2) To dummy
.
.
Where FunctionAddress is taken from the structure. I even tried double-dereferencing the address, but that's not correct either. Even if I try to walk through the pointers in the (MS C++) debugger by hand, I can't get myself to anything resembling a function.
I wouldn't be asking this question if i thought that macroMedia were doing something really wierd. They supply the sample code for direct plugging into C comilers, so the syntax and usage is quite standard...
Can anybody help?
John
The 'C' looks pretty simple, but the obvious conversion isn't the correct one!
The 'C' (stripped down a bit for readability) is:
'-----------------------------------------------------------
typedef long JSBool;
typedef JSBool (*JSNative)(JSContext *cx, JSObject *obj, unsigned int argc,
jsval *argv, jsval *rval);
/* JSBool JS_DefineFunction(char *name, JSNative call, unsigned int nargs) */
#define JS_DefineFunction(n, c, a) \
(mmEnv.defineFunction ? (*(mmEnv.defineFunction))(mmEnv.libObj, n, c, a) \
: JS_FALSE)
typedef struct {
JSObject *libObj;
JSBool (*defineFunction)(JSObject *libObj, char *name, JSNative call,
unsigned int nargs);
.
.
} MM_Environment;
extern MM_Environment mmEnv;
.
.
.
MM_Init()
{
JS_DefineFunction("computeSum", computeSum, 2);
}
JSBool
computeSum(JSContext *cx, JSObject *obj, unsigned int argc,
jsval *argv, jsval *rval)
{
.
.
.
return JS_TRUE;
}
'-----------------------------------------------------
O.k., so JSNative is simply a pointer to a function with 5 arguments - no problem.
I start to run into problem with the simple-looking line (above):
JS_DefineFunction("computeSum", computeSum, 2);
I realise that this function call refers to the previous typedef, so it actually has 4 parameters... a pointer to an object, a pointer to a string, a pointer to a function and an integer.
Now then, the problems begin. Dreamweaver passes us a pointer to the content of the MMenv structure and lets us copy it to a local structure. I assumed that to call the function, all I needed to do was to take the second DWORD in the structure as a pointer to the function, and to use CALL DWORD.
My code is:
Declare Function JS_DefineFunction Alias "JS_DefineFunction" (JSObject As Dword,FunctionName As Asciiz, JSNative As Dword, ByVal nargs As Integer) As Long
.
.
.
ComputeSumPointer=CodePtr(ComputeSum)
Call Dword FunctionAddress Using JS_DefineFunction
(MM_Environment.libobj,"ComputeSum", ComputeSumPointer, 2) To dummy
.
.
Where FunctionAddress is taken from the structure. I even tried double-dereferencing the address, but that's not correct either. Even if I try to walk through the pointers in the (MS C++) debugger by hand, I can't get myself to anything resembling a function.
I wouldn't be asking this question if i thought that macroMedia were doing something really wierd. They supply the sample code for direct plugging into C comilers, so the syntax and usage is quite standard...
Can anybody help?
John
Comment