本文整理汇总了C++中JS_GetStringLength函数的典型用法代码示例。如果您正苦于以下问题:C++ JS_GetStringLength函数的具体用法?C++ JS_GetStringLength怎么用?C++ JS_GetStringLength使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了JS_GetStringLength函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Print
static JSBool
Print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsJSSh* shell;
if (!GetJSShGlobal(cx, obj, &shell)) return JS_FALSE;
JSAutoRequest ar(cx);
PRUint32 bytesWritten;
#ifdef DEBUG
// nsCOMPtr<nsIThread> thread;
// nsIThread::GetCurrent(getter_AddRefs(thread));
// printf("printing on thread %p, shell %p, output %p, cx=%p, obj=%p\n", thread.get(), shell, shell->mOutput, cx, obj);
#endif
for (unsigned int i=0; i<argc; ++i) {
JSString *str = JS_ValueToString(cx, argv[i]);
if (!str) return JS_FALSE;
if (shell->mOutput) {
if (shell->mEmitHeader) {
char buf[80];
sprintf(buf, "[%d]", JS_GetStringLength(str));
shell->mOutput->Write(buf, strlen(buf), &bytesWritten);
}
shell->mOutput->Write(JS_GetStringBytes(str), JS_GetStringLength(str), &bytesWritten);
}
else
printf("%s", JS_GetStringBytes(str)); // use cout if no output stream given.
#ifdef DEBUG
// printf("%s", JS_GetStringBytes(str));
#endif
}
return JS_TRUE;
}
示例2: NS_ENSURE_ARG_POINTER
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
NS_IMETHODIMP
calDateTime::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
JSObject * obj, jsval id, PRUint32 flags,
JSObject * *objp, PRBool *_retval)
{
NS_ENSURE_ARG_POINTER(objp);
NS_ENSURE_ARG_POINTER(_retval);
if (JSVAL_IS_STRING(id)) {
JSString *str = JSVAL_TO_STRING(id);
nsDependentString const name(
reinterpret_cast<PRUnichar const*>(JS_GetStringChars(str)),
JS_GetStringLength(str));
if (name.EqualsLiteral("jsDate")) {
*_retval = JS_DefineUCProperty(cx, obj, JS_GetStringChars(str),
JS_GetStringLength(str),
JSVAL_VOID,
nsnull, nsnull, 0);
*objp = obj;
return *_retval ? NS_OK : NS_ERROR_FAILURE;
}
}
*_retval = PR_TRUE;
return NS_OK;
}
示例3: mapFromJSObject
/**
* Convert a JavaScript Object to a map
*
* @param cx the JavaScript context
* @param t the JavaScript Object to convert
* @return a new map containing the JavaScript Object
*/
map* mapFromJSObject(JSContext *cx,jsval t){
map *res=NULL;
JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t));
#ifdef JS_DEBUG
fprintf(stderr,"Properties %p\n",(void*)t);
#endif
if(idp!=NULL) {
int index;
jsdouble argNum;
#ifdef JS_DEBUG
fprintf(stderr,"Properties length : %d \n",idp->length);
#endif
for (index=0,argNum=idp->length;index<argNum;index++) {
jsval id = idp->vector[index];
jsval vp;
JS_IdToValue(cx,id,&vp);
char *tmp, *tmp1;
JSString *jsmsg,*jsmsg1;
size_t len,len1;
jsmsg = JS_ValueToString(cx,vp);
len = JS_GetStringLength(jsmsg);
jsval nvp;
tmp=JS_EncodeString(cx,jsmsg);
JS_GetProperty(cx, JSVAL_TO_OBJECT(t), tmp, &nvp);
jsmsg1 = JS_ValueToString(cx,nvp);
len1 = JS_GetStringLength(jsmsg1);
tmp1=JS_EncodeString(cx,jsmsg1);
#ifdef JS_DEBUG
fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,tmp,tmp1);
#endif
if(strcasecmp(tmp,"child")!=0){
if(res!=NULL){
#ifdef JS_DEBUG
fprintf(stderr,"%s - %s\n",tmp,tmp1);
#endif
addToMap(res,tmp,tmp1);
}
else{
res=createMap(tmp,tmp1);
res->next=NULL;
}
}
free(tmp);
free(tmp1);
#ifdef JS_DEBUG
dumpMap(res);
#endif
}
JS_DestroyIdArray(cx,idp);
}
#ifdef JS_DEBUG
dumpMap(res);
#endif
return res;
}
示例4: to_erl_object
int
to_erl_object(emonk_buf_t* buf, JSContext* cx, JSObject* obj)
{
JSObject* iter;
JSString* key;
jsid idp;
jsval val;
jschar* keyname;
size_t keylen;
int count = 0;
int lengthpos;
REQUEST(7);
BUFPTR[0] = SMALL_TUPLE;
BUFPTR[1] = (char) 1;
BUFPTR[2] = LIST;
// Remember the byte offset where length goes so we can write it
// after enumerating the properties.
lengthpos = buf->used + 3;
buf->used += 7;
iter = JS_NewPropertyIterator(cx, obj);
if(iter == NULL) return ERROR;
while(JS_NextProperty(cx, iter, &idp))
{
// Done iterating, write length and bail.
if(idp == JSVAL_VOID)
{
count = htonl(count);
memcpy(buf->buf+lengthpos, &count, 4);
REQUEST(1);
BUFPTR[0] = NIL;
buf->used += 1;
return OK;
}
REQUEST(2);
BUFPTR[0] = SMALL_TUPLE;
BUFPTR[1] = 2;
buf->used += 2;
if(!JS_IdToValue(cx, idp, &val)) return ERROR;
if(!to_erl_string(buf, cx, val)) return ERROR;
key = JS_ValueToString(cx, val);
keyname = JS_GetStringChars(key);
keylen = JS_GetStringLength(key);
if(!JS_GetUCProperty(cx, obj, keyname, keylen, &val)) return ERROR;
if(!to_erl_intern(buf, cx, val)) return ERROR;
count += 1;
}
return ERROR;
}
示例5: Btoa
Btoa(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString *str;
if (!argc)
return JS_TRUE;
str = JS_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
char *bin_data = JS_GetStringBytes(str);
size_t bin_dataLength = JS_GetStringLength(str);
char *base64 = PL_Base64Encode(bin_data, bin_dataLength, nsnull);
if (!base64)
return JS_FALSE;
PRUint32 base64Length = ((bin_dataLength + 2) / 3) * 4;
str = JS_NewStringCopyN(cx, base64, base64Length);
PR_Free(base64);
if (!str)
return JS_FALSE;
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
示例6: Atob
Atob(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString *str;
if (!argc)
return JS_TRUE;
str = JS_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
size_t base64StrLength = JS_GetStringLength(str);
char *base64Str = JS_GetStringBytes(str);
PRUint32 bin_dataLength = (PRUint32)base64StrLength;
if (base64Str[base64StrLength - 1] == '=') {
if (base64Str[base64StrLength - 2] == '=')
bin_dataLength -= 2;
else
--bin_dataLength;
}
bin_dataLength = (PRUint32)((PRUint64)bin_dataLength * 3) / 4;
char *bin_data = PL_Base64Decode(base64Str, base64StrLength, nsnull);
if (!bin_data)
return JS_FALSE;
str = JS_NewStringCopyN(cx, bin_data, bin_dataLength);
PR_Free(bin_data);
if (!str)
return JS_FALSE;
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
示例7: wimg_image_info_setter
JSBool wimg_image_info_setter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp)
{
JS_BeginRequest(cx);
HANDLE hImage = JS_GetPrivate(cx, obj);
JSString * newInfo = JS_ValueToString(cx, *vp);
*vp = STRING_TO_JSVAL(newInfo);
DWORD length = JS_GetStringLength(newInfo);
LPWSTR chars = (LPWSTR)JS_GetStringChars(newInfo);
if(*chars != 0xfeff)
{
length++;
LPWSTR back = chars;
chars = (LPWSTR)JS_malloc(cx, sizeof(WCHAR) * (length + 1));
memset(chars, 0, sizeof(WCHAR) * (length + 1));
*chars = 0xfeff;
wcscat(chars, back);
newInfo = JS_NewUCString(cx, (jschar*)chars, sizeof(WCHAR) * length);
*vp = STRING_TO_JSVAL(newInfo);
}
DWORD errorCode = 0;
if(!WIMSetImageInformation(hImage, (LPVOID)chars, length * sizeof(WCHAR)))
errorCode = GetLastError();
JS_EndRequest(cx);
return JS_TRUE;
}
示例8: wimg_image_info_getter
JSBool wimg_image_info_getter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp)
{
JS_BeginRequest(cx);
HANDLE hImage = JS_GetPrivate(cx, obj);
LPVOID rawImageInfo;
DWORD rawImageInfoSize;
if(!WIMGetImageInformation(hImage, &rawImageInfo, &rawImageInfoSize))
*vp = JSVAL_NULL;
JSString * xmlStr = JS_NewUCStringCopyN(cx, (jschar*)rawImageInfo, rawImageInfoSize / sizeof(WCHAR));
if(xmlStr == NULL)
{
JS_ReportError(cx, "Error creating xml string from raw image information");
JS_EndRequest(cx);
return JS_FALSE;
}
jsval xmlStrVal = STRING_TO_JSVAL(xmlStr);
*vp = xmlStrVal;
LocalFree(rawImageInfo);
WIMSetImageInformation(hImage, JS_GetStringChars(xmlStr), JS_GetStringLength(xmlStr) * sizeof(jschar));
JS_EndRequest(cx);
return JS_TRUE;
}
示例9: JSVAL_TO_STRING
xpc_qsACString::xpc_qsACString(JSContext *cx, jsval *pval)
{
// From the T_CSTRING case in XPCConvert::JSData2Native.
jsval v;
JSString *s;
v = *pval;
if(JSVAL_IS_STRING(v))
{
s = JSVAL_TO_STRING(v);
}
else
{
if(JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
{
(new(mBuf) implementation_type())->SetIsVoid(PR_TRUE);
mValid = JS_TRUE;
return;
}
s = JS_ValueToString(cx, v);
if(!s)
{
mValid = JS_FALSE;
return;
}
*pval = STRING_TO_JSVAL(s); // Root the new string.
}
const char *bytes = JS_GetStringBytes(s);
size_t len = JS_GetStringLength(s);
new(mBuf) implementation_type(bytes, len);
mValid = JS_TRUE;
}
示例10: jsWrite
static JSBool
jsWrite( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval )
{
*rval = JSVAL_FALSE ;
jsUsblpPoll_t *dev = (jsUsblpPoll_t *)JS_GetInstancePrivate( cx, obj, &jsUsblpClass_, NULL );
if( dev )
{
unsigned totalLength = 0 ;
for( uintN arg = 0 ; arg < argc ; arg++ )
{
JSString *str = JS_ValueToString(cx, argv[arg]);
if( str ) {
unsigned const segLength = JS_GetStringLength( str );
char const *outData = JS_GetStringBytes( str );
int numWritten = dev->write( outData, segLength );
if( 0 < numWritten ) {
totalLength += numWritten ;
}
else if( 0 > numWritten ) {
JS_ReportError( cx, "usblp: short write %d of %u\n", numWritten, segLength );
}
}
else
JS_ReportError( cx, "usblp.write() - error converting to string\n" );
}
*rval = INT_TO_JSVAL(totalLength);
}
else
JS_ReportError( cx, "Invalid usblp object\n" );
return JS_TRUE ;
}
示例11: PJS_JSString2SV
SV *
PJS_JSString2SV(
pTHX_
JSString *jstr
) {
SV *ret;
#if PJS_UTF8_NATIVE
char *str = JS_GetStringBytes(jstr);
ret = newSVpv(str, 0);
SvUTF8_on(ret);
#else
dSP;
jschar *chars = JS_GetStringChars(jstr);
SV *esv = newSVpv((char *)chars, JS_GetStringLength(jstr) * sizeof(jschar));
ENTER; SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(PJS_STR_ENCODING, 0)));
XPUSHs(sv_2mortal(esv));
PUTBACK;
call_pv("Encode::decode", G_SCALAR);
SPAGAIN;
ret = newSVsv(POPs);
PUTBACK;
FREETMPS; LEAVE;
// sv_utf8_downgrade(ret, 1); Its safe, but pays the cost?
#endif
return ret;
}
示例12: JS_GetGlobalObject
nsresult
nsDOMWorkerTimeout::ExpressionCallback::Run(nsDOMWorkerTimeout* aTimeout,
JSContext* aCx)
{
JSObject* global = JS_GetGlobalObject(aCx);
NS_ENSURE_TRUE(global, NS_ERROR_FAILURE);
JSPrincipals* principal = nsDOMWorkerSecurityManager::WorkerPrincipal();
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
JSString* expression = JS_ValueToString(aCx, mExpression);
NS_ENSURE_TRUE(expression, NS_ERROR_FAILURE);
jschar* string = JS_GetStringChars(expression);
NS_ENSURE_TRUE(string, NS_ERROR_FAILURE);
size_t stringLength = JS_GetStringLength(expression);
jsval rval;
PRBool success = JS_EvaluateUCScriptForPrincipals(aCx, global, principal,
string, stringLength,
mFileName.get(),
mLineNumber, &rval);
if (!success) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
示例13: jsSend
static JSBool
jsSend( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval )
{
*rval = JSVAL_FALSE ;
jsPort_t *params = (jsPort_t *)JS_GetInstancePrivate( cx, obj, &jsSerialPortClass_, NULL );
if( params )
{
if( 1 == argc )
{
JSString *jsMsg = JSVAL_TO_STRING( argv[0] );
char * p = JS_GetStringBytes( jsMsg );
int len = JS_GetStringLength( jsMsg );
int numWritten;
if (params->outputDelay()==0)
numWritten = write( params->getFd(), p, len );
else {
numWritten=0;
while (len) {
usleep(params->outputDelay() );
numWritten += write( params->getFd(), p, 1 );
p++;
len--;
}
}
*rval = INT_TO_JSVAL( numWritten );
}
else
JS_ReportError( cx, "Usage: sendToScanner( \'string\' );" );
}
return JS_TRUE ;
}
示例14: js_json_parse
static JSBool
js_json_parse(JSContext *cx, JSObject* pThis, uintN argc, jsval *vp, jsval* rval)
{
JSString *s = NULL;
jsval *argv = vp;
// Must throw an Error if there isn't a first arg
if (!JS_ConvertArguments(cx, argc, argv, "S", &s))
return JS_FALSE;
JSBool ok = JS_TRUE;
JSONParser *jp = js_BeginJSONParse(cx, rval);
if (!jp)
ok = JS_FALSE;
if (ok) {
ok = js_ConsumeJSONText(cx, jp, JS_GetStringChars(s), JS_GetStringLength(s));
ok &= js_FinishJSONParse(cx, jp);
}
if (!ok)
JS_ReportError(cx, "Error parsing JSON.");
return ok;
}
示例15: Context_execute
PyObject*
Context_execute(Context* self, PyObject* args, PyObject* kwargs)
{
PyObject* obj = NULL;
PyObject* ret = NULL;
JSContext* cx = NULL;
JSObject* root = NULL;
JSString* script = NULL;
jschar* schars = NULL;
JSBool started_counter = JS_FALSE;
size_t slen;
jsval rval;
JS_BeginRequest(self->cx);
if(!PyArg_ParseTuple(args, "O", &obj)) goto error;
script = py2js_string_obj(self, obj);
if(script == NULL) goto error;
schars = JS_GetStringChars(script);
slen = JS_GetStringLength(script);
cx = self->cx;
root = self->root;
// Mark us for time consumption
if(self->start_time == 0)
{
started_counter = JS_TRUE;
self->start_time = time(NULL);
}
if(!JS_EvaluateUCScript(cx, root, schars, slen, "<JavaScript>", 1, &rval))
{
if(!PyErr_Occurred())
{
PyErr_SetString(PyExc_RuntimeError, "Failed to execute script.");
}
goto error;
}
if(PyErr_Occurred()) goto error;
ret = js2py(self, rval);
JS_EndRequest(self->cx);
JS_MaybeGC(self->cx);
goto success;
error:
JS_EndRequest(self->cx);
success:
if(started_counter)
{
self->start_time = 0;
}
return ret;
}