当前位置: 首页>>代码示例>>C++>>正文


C++ JS_malloc函数代码示例

本文整理汇总了C++中JS_malloc函数的典型用法代码示例。如果您正苦于以下问题:C++ JS_malloc函数的具体用法?C++ JS_malloc怎么用?C++ JS_malloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了JS_malloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: js_InitNavigatorClass

JSObject *
js_InitNavigatorClass(JSContext *cx, JSObject *obj) {
	
	JSObject *Navigator;

	Navigator = JS_DefineObject(cx, obj, "navigator", &navigator_class, NULL, 0);
  if (!Navigator)
  	return NULL;
  if (!JS_DefineFunctions(cx, Navigator, navigator_static_methods))
  	return NULL;
  if (!JS_DefineProperties(cx, Navigator, navigator_properties))
  	return NULL;

    CMNavigator *cmNav;

   cmNav = JS_malloc(cx, sizeof *cmNav);
  if (!cmNav)
     return NULL;
  memset(cmNav, 0 , sizeof *cmNav);

  cmNav->appVersion = JS_malloc(cx, strlen("Firefox/2"));
  cmNav->appVersion = strdup("Firefox/2");

JS_SetPrivate(cx, Navigator, cmNav);

  return Navigator;
}
开发者ID:pankajtanwar,项目名称:ponkey,代码行数:27,代码来源:jsnavigator.c

示例2: JS_SetTrap

JS_SetTrap(JSContext *cx, JSScript *script, jsbytecode *pc,
           JSTrapHandler handler, void *closure)
{
    JSRuntime *rt;
    JSTrap *trap;

    rt = cx->runtime;
    trap = FindTrap(rt, script, pc);
    if (trap) {
        /* Restore opcode at pc so it can be saved again. */
        *pc = (jsbytecode)trap->op;
    } else {
        trap = (JSTrap *) JS_malloc(cx, sizeof *trap);
        if (!trap || !js_AddRoot(cx, &trap->closure, "trap->closure")) {
            if (trap)
                JS_free(cx, trap);
            return JS_FALSE;
        }
    }
    JS_APPEND_LINK(&trap->links, &rt->trapList);
    trap->script = script;
    trap->pc = pc;
    trap->op = (JSOp)*pc;
    trap->handler = handler;
    trap->closure = closure;
    *pc = JSOP_TRAP;
    return JS_TRUE;
}
开发者ID:DmitrySigaev,项目名称:DSMedia,代码行数:28,代码来源:jsdbgapi.c

示例3: 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;

}
开发者ID:z4y4,项目名称:njord,代码行数:29,代码来源:js_wimg.cpp

示例4: sizeof

// static
JSString*
DOMProxyHandler::obj_toString(JSContext* cx, const char* className)
{
  size_t nchars = sizeof("[object ]") - 1 + strlen(className);
  jschar* chars = static_cast<jschar*>(JS_malloc(cx, (nchars + 1) * sizeof(jschar)));
  if (!chars) {
    return NULL;
  }

  const char* prefix = "[object ";
  nchars = 0;
  while ((chars[nchars] = (jschar)*prefix) != 0) {
    nchars++, prefix++;
  }
  while ((chars[nchars] = (jschar)*className) != 0) {
    nchars++, className++;
  }
  chars[nchars++] = ']';
  chars[nchars] = 0;

  JSString* str = JS_NewUCString(cx, chars, nchars);
  if (!str) {
    JS_free(cx, chars);
  }
  return str;
}
开发者ID:petemoore,项目名称:mozilla-central,代码行数:27,代码来源:DOMJSProxyHandler.cpp

示例5: JS_SetTrap

JS_SetTrap(JSContext *cx, JSScript *script, jsbytecode *pc,
           JSTrapHandler handler, void *closure)
{
    JSRuntime *rt;
    JSTrap *trap;

    rt = cx->runtime;
    trap = FindTrap(rt, script, pc);
    if (trap) {
        JS_ASSERT(trap->script == script && trap->pc == pc);
        JS_ASSERT(*pc == JSOP_TRAP);
    } else {
        trap = (JSTrap *) JS_malloc(cx, sizeof *trap);
        if (!trap || !js_AddRoot(cx, &trap->closure, "trap->closure")) {
            if (trap)
                JS_free(cx, trap);
            return JS_FALSE;
        }
        JS_APPEND_LINK(&trap->links, &rt->trapList);
        trap->script = script;
        trap->pc = pc;
        trap->op = (JSOp)*pc;
        *pc = JSOP_TRAP;
    }
    trap->handler = handler;
    trap->closure = closure;
    return JS_TRUE;
}
开发者ID:bluecherrydvr,项目名称:omv,代码行数:28,代码来源:jsdbgapi.c

示例6: gpsee_throw

/** Throw an exception from our C code to be caught by the 
 *  running JavaScript program.
 *
 *  To use this function, in most cases, do:
 *  <pre>
 *  if (badness)
 *    return gpsee_throw(cx, "bad things just happened");
 *  </pre>
 *
 *  @param	cx	Context from which to device runtime information
 *  @param	fmt	printf-style format for the text to throw as an exception
 *  @param	...	printf-style arguments to fmt
 *
 *  @returns	JS_FALSE
 *
 *  @warning	This function will panic if the runtime cannot allocate 
 *		enough memory to prepare the exception-throwing message.
 *
 *  @note	This function uses the APR SureLynx gpsee_makeLogFormat() function,
 *		so %m will work, and so will addPercentM_Handler() with surelynx.
 */
JSBool gpsee_throw(JSContext *cx, const char *fmt, ...)
{
  char 		*message;
  va_list	ap;
  size_t	length;
  char		fmtNew[GPSEE_MAX_LOG_MESSAGE_SIZE];

  message = JS_malloc(cx, GPSEE_MAX_THROW_MESSAGE_SIZE);
  if (!message)
    panic(GPSEE_GLOBAL_NAMESPACE_NAME ": out of memory in gpsee_throw!");

  va_start(ap, fmt);
  length = vsnprintf(message, GPSEE_MAX_THROW_MESSAGE_SIZE, gpsee_makeLogFormat(fmt, fmtNew), ap);
  va_end(ap);

  if (length <= 0)
  {
    gpsee_cpystrn(message, "JS Engine unnamed exception", GPSEE_MAX_THROW_MESSAGE_SIZE);
    length = strlen(message);
  }

  if (JS_IsExceptionPending(cx) == JS_TRUE)
    gpsee_log(cx, GLOG_ERR, GPSEE_GLOBAL_NAMESPACE_NAME ": Already throwing an exception; not throwing '%s'!", message);
  else
    JS_ReportError(cx, "%s", message);

  return JS_FALSE;
}
开发者ID:wesgarland,项目名称:gpsee,代码行数:49,代码来源:gpsee.c

示例7: win32_getenv

JSBool win32_getenv(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
{
	JS_BeginRequest(cx);
	if(argc < 1)
	{
		JS_ReportError(cx, "getenv requires the name of the variable to be passed");
		JS_EndRequest(cx);
		return JS_FALSE;
	}
	JSString * name = JS_ValueToString(cx, argv[0]);
	DWORD size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), NULL, 0);
	if(size == 0)
	{
		*rval = JSVAL_FALSE;
		JS_EndRequest(cx);
		return JS_TRUE;
	}
	LPWSTR valueBuffer = (LPWSTR)JS_malloc(cx, (size + 2) * sizeof(WCHAR));
	size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), valueBuffer, size + 1);
	if(size == 0)
	{
		JS_free(cx, valueBuffer);
		*rval = JSVAL_FALSE;
		JS_EndRequest(cx);
		return JS_TRUE;
	}
	JSString * value = JS_NewUCString(cx, valueBuffer, size + 1);
	*rval = STRING_TO_JSVAL(value);
	JS_EndRequest(cx);
	return JS_TRUE;
}
开发者ID:z4y4,项目名称:njord,代码行数:31,代码来源:win32s.cpp

示例8: JSGET_PTRSHELL

JSBool S_CCMenuItemLabel::jsitemWithLabel(JSContext *cx, uint32_t argc, jsval *vp) {
    JSObject* obj = (JSObject *)JS_THIS_OBJECT(cx, vp);
    S_CCRenderTexture* self = NULL; JSGET_PTRSHELL(S_CCRenderTexture, self, obj);
    if (self == NULL) return JS_FALSE;
    if (argc == 1) {
        JSObject *arg0;
        JS_ConvertArguments(cx, 1, JS_ARGV(cx, vp), "o", &arg0);
        CCNode* narg0; JSGET_PTRSHELL(CCNode, narg0, arg0);
        CCMenuItemLabel *ret = CCMenuItemLabel::itemWithLabel(narg0, self, menu_selector(S_CCMenuItemLabel::menuAction));
        if (ret == NULL) {
            JS_SET_RVAL(cx, vp, JSVAL_NULL);
            return JS_TRUE;
        }
        do {
            JSObject *tmp = JS_NewObject(cx, S_CCMenuItemLabel::jsClass, S_CCMenuItemLabel::jsObject, NULL);
            pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t));
            pt->flags = kPointerTemporary;
            pt->data = (void *)ret;
            JS_SetPrivate(tmp, pt);
            JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(tmp));
        } while(0);
        
        return JS_TRUE;
    }
    JS_SET_RVAL(cx, vp, JSVAL_TRUE);
    return JS_TRUE;
}
开发者ID:136446529,项目名称:book-code,代码行数:27,代码来源:cocos2d_manual_bindings.cpp

示例9: jsRead

static JSBool
jsRead( 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 numAvail = dev->bytesAvail();
        if( 0 < numAvail ) {
            char *const inBuf = (char *)JS_malloc( cx, numAvail+1 );
            unsigned numRead ;
            if( dev->read( inBuf, numAvail, numRead ) ) {
                JSString *sData = JS_NewString( cx, inBuf, numRead );
                *rval = STRING_TO_JSVAL( sData );
            }
            else {
                JS_ReportError( cx, "usblp read error\n" );
                JS_free(cx, inBuf);
            }
        }
    }
    else
        JS_ReportError( cx, "Invalid usblp object\n" );
    return JS_TRUE ;
}
开发者ID:boundarydevices,项目名称:bdScript,代码行数:25,代码来源:jsUsblp.cpp

示例10: JS_ConvertArguments

JSBool S_CCLabelTTF::jslabelWithString(JSContext *cx, uint32_t argc, jsval *vp) {
    if (argc == 5) {
        JSString *arg0;
        JSObject *arg1;
        int arg2;
        JSString *arg3;
        double arg4;
        JS_ConvertArguments(cx, 5, JS_ARGV(cx, vp), "SoiSd", &arg0, &arg1, &arg2, &arg3, &arg4);
        char *narg0 = JS_EncodeString(cx, arg0);
        CCSize* narg1; JSGET_PTRSHELL(CCSize, narg1, arg1);
        char *narg3 = JS_EncodeString(cx, arg3);
        CCLabelTTF *ret = CCLabelTTF::labelWithString(narg0, *narg1, (CCTextAlignment)arg2, narg3, arg4);
        if (ret == NULL) {
            JS_SET_RVAL(cx, vp, JSVAL_NULL);
            return JS_TRUE;
        }
        do {
            JSObject *tmp = JS_NewObject(cx, S_CCLabelTTF::jsClass, S_CCLabelTTF::jsObject, NULL);
            pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t));
            pt->flags = kPointerTemporary;
            pt->data = (void *)ret;
            JS_SetPrivate(tmp, pt);
            JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(tmp));
        } while (0);
        return JS_TRUE;
    }
    JS_SET_RVAL(cx, vp, JSVAL_TRUE);
    return JS_TRUE;
}
开发者ID:136446529,项目名称:book-code,代码行数:29,代码来源:cocos2d_manual_bindings.cpp

示例11: JS_XDRString

JS_XDRString(JSXDRState *xdr, JSString **strp)
{
    uint32 nchars;
    jschar *chars;

    if (xdr->mode == JSXDR_ENCODE)
        nchars = JSSTRING_LENGTH(*strp);
    if (!JS_XDRUint32(xdr, &nchars))
        return JS_FALSE;

    if (xdr->mode == JSXDR_DECODE) {
        chars = (jschar *) JS_malloc(xdr->cx, (nchars + 1) * sizeof(jschar));
        if (!chars)
            return JS_FALSE;
    } else {
        chars = JSSTRING_CHARS(*strp);
    }

    if (!XDRChars(xdr, chars, nchars))
        goto bad;
    if (xdr->mode == JSXDR_DECODE) {
        chars[nchars] = 0;
        *strp = JS_NewUCString(xdr->cx, chars, nchars);
        if (!*strp)
            goto bad;
    }
    return JS_TRUE;

bad:
    if (xdr->mode == JSXDR_DECODE)
        JS_free(xdr->cx, chars);
    return JS_FALSE;
}
开发者ID:2812140729,项目名称:robomongo,代码行数:33,代码来源:jsxdrapi.c

示例12: recv_body

static size_t
recv_body(void *ptr, size_t size, size_t nmem, void *data)
{
    CurlState* state = (CurlState*) data;
    size_t length = size * nmem;
    char* tmp = NULL;
    
    if(!state->recvbuf) {
        state->recvlen = 4096;
        state->read = 0;
        state->recvbuf = JS_malloc(state->cx, state->recvlen);
    }
    
    if(!state->recvbuf) {
        return CURLE_WRITE_ERROR;
    }

    // +1 so we can add '\0' back up in the go function.
    while(length+1 > state->recvlen - state->read) state->recvlen *= 2;
    tmp = JS_realloc(state->cx, state->recvbuf, state->recvlen);
    if(!tmp) return CURLE_WRITE_ERROR;
    state->recvbuf = tmp;
   
    memcpy(state->recvbuf + state->read, ptr, length);
    state->read += length;
    return length;
}
开发者ID:AvianFlu,项目名称:couchdb,代码行数:27,代码来源:http.c

示例13: __Window_readLine

JSString*
__Window_readLine (JSContext* cx, WINDOW* win, JSBool moveFirst, jsval x, jsval y)
{
    char* string  = (char*) JS_malloc(cx, 16*sizeof(char));
    size_t length = 0;

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    string[0] = (moveFirst
        ? mvwgetch(win, JSVAL_TO_INT(y), JSVAL_TO_INT(x))
        : wgetch(win));
    
    while (string[(++length)-1] != '\n') {
        if ((length+1) % 16) {
            string = (char*) JS_realloc(cx, string, (length+16+1)*sizeof(char));
        }
    
        string[length] = (char) wgetch(win);
    }

    string[length-1] = '\0';
    string = (char*) JS_realloc(cx, string, length*sizeof(char));

    JSString* jsString = JS_NewString(cx, string, strlen(string));

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
    return jsString;
}
开发者ID:meh,项目名称:lulzjs-ncurses,代码行数:30,代码来源:Window.cpp

示例14: JS_ARGV

JSBool S_CCSequence::jsactions(JSContext *cx, uint32_t argc, jsval *vp) {
    // just like CCSequence::actions
    if (argc > 0) {
        jsval* argv = JS_ARGV(cx, vp);
        // get first element
        S_CCSequence* prev;
        JSGET_PTRSHELL(S_CCSequence, prev, JSVAL_TO_OBJECT(argv[0]));
        for (int i=1; i < argc; i++) {
            CCFiniteTimeAction *next; JSGET_PTRSHELL(CCFiniteTimeAction, next, JSVAL_TO_OBJECT(argv[i]));
            prev = (S_CCSequence *)CCSequence::actionOneTwo(prev, next);
        }
        // wrap prev in an action
        // temporary because it's just a wrapper for an autoreleased object
        // or worst case, it's an already binded object (if it's just one item in the array)
        pointerShell_t* pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t));
        pt->flags = kPointerTemporary;
        pt->data = prev;
        JSObject* out = JS_NewObject(cx, S_CCSequence::jsClass, S_CCSequence::jsObject, NULL);
        prev->jsObject = out;
        JS_SetPrivate(out, pt);
        JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(out));
        return JS_TRUE;
    }
    JS_ReportError(cx, "must call with at least one element");
    return JS_FALSE;
}
开发者ID:136446529,项目名称:book-code,代码行数:26,代码来源:cocos2d_manual_bindings.cpp

示例15: enc_string

char*
enc_string(JSContext* cx, jsval arg, size_t* buflen)
{
    JSString* str = NULL;
    jschar* src = NULL;
    char* bytes = NULL;
    size_t srclen = 0;
    size_t byteslen = 0;

    str = JS_ValueToString(cx, arg);
    if(!str) goto error;

    src = JS_GetStringChars(str);
    srclen = JS_GetStringLength(str);

    if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error;

    bytes = JS_malloc(cx, (byteslen) + 1);
    bytes[byteslen] = 0;

    if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error;

    if(buflen) *buflen = byteslen;
    goto success;

error:
    if(bytes != NULL) JS_free(cx, bytes);
    bytes = NULL;

success:
    return bytes;
}
开发者ID:benoitc,项目名称:couchjs,代码行数:32,代码来源:utf8.c


注:本文中的JS_malloc函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。