Click to See Complete Forum and Search --> : JavaScript to C++. The simplest thing


Sasqatch
July 30th, 2004, 05:55 PM
I'm sure that is very simple problem but i'm only beginner in C++, so i'll be very thankful for any advice.

I have some functions that manadge passing variables from JS to C and back (if be more correct is about JSFL in Flash MX 2004 Extensibility) and need convert JavaScript string to C/C++ char * data type (path to a file).
There is the special function JS_ValueToString which gets JS value (js string) convert it and returns a pointer that points to a null-terminated string. So a past several days i'm trying apply JavaScript programming knowledge but with no success I've learned about pointers in C++ a little, but seems that this case differs from pointers to variables.

Below is the code i'm trying to finish:


extern "C"
{
#include <TChar.h>
#include "mm_jsapi.h"

JSBool testFunc(JSContext *cx, JSObject *obj, unsigned int argc, jsval *argv, jsval *rval)
{
unsigned int leng;
unsigned short p;
char * my_str;

p = *JS_ValueToString(cx, argv[0], &leng);

my_str = ???

return JS_TRUE;
}
}


Just in case here is JS_ValueToString explanation from docs:


unsigned short *JS_ValueToString()

Usage
unsigned short *JS_ValueToString(JSContext *cx, jsval v, unsigned int *pLength)

Description
This function extracts a function argument from a jsval structure, converts it to a string, if possible, and passes the converted value back to the caller.

Note: Do not modify the returned buffer pointer or you might corrupt the data structures of the JavaScript interpreter. To change the string, you must copy the characters into another buffer and create a new JavaScript string.

Arguments
JSContext *cx, jsval v, unsigned int *pLength

The cx argument is the opaque JSContext pointer that passes to the JavaScript function.
The v argument is the jsval structure from which the string is to be extracted.
The pLength argument is a pointer to an unsigned integer. This function sets *plength equal to the length of the string in bytes.

Returns
A pointer that points to a null-terminated string if successful or to a null value on failure. The calling routine must not free this string when it finishes.


THANK YOU FOR ANY ADVICE!