本文整理汇总了C++中STRING_TO_JSVAL函数的典型用法代码示例。如果您正苦于以下问题:C++ STRING_TO_JSVAL函数的具体用法?C++ STRING_TO_JSVAL怎么用?C++ STRING_TO_JSVAL使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了STRING_TO_JSVAL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: jwindow_dialog
static JSBool jwindow_dialog(JSContext *cx, JSObject *obj,uintN argc, jsval *argv, jsval *rval, int which_dialogue)
{
WebcJSDocumentContext *jsmgr = (WebcJSDocumentContext *) jhutil_GetPrivate(cx, obj);
HTMLDocument *pDoc = (jsmgr)? jsmgr->GetDocument() : 0;
if (argc < 1)
return(JS_FALSE);
char *prompt_str = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
// << EDIT 20080120 - Needs work
// Clone the graphics abstraction layer function pointers so we know who to call
HTMLBrowserConfig config, docConfig;
rtp_memset(&config, 0, sizeof(config));
rtp_memset(&docConfig, 0, sizeof(docConfig));
pDoc->GetBrowser()->GetConfig(&docConfig);
config.graphics = docConfig.graphics;
switch (which_dialogue) {
case WHICH_DIALOGUE_ALERT:
config.WindowConfig.left = WEBC_SKIN_ALERT_LEFT;
config.WindowConfig.top = WEBC_SKIN_ALERT_TOP;
config.WindowConfig.width = WEBC_SKIN_ALERT_WIDTH;
config.WindowConfig.height = WEBC_SKIN_ALERT_HEIGHT;
break;
case WHICH_DIALOGUE_PROMPT:
config.WindowConfig.left = WEBC_SKIN_PROMPT_LEFT;
config.WindowConfig.top = WEBC_SKIN_PROMPT_TOP;
config.WindowConfig.width = WEBC_SKIN_PROMPT_WIDTH;
config.WindowConfig.height = WEBC_SKIN_PROMPT_HEIGHT;
break;
case WHICH_DIALOGUE_CONFIRM:
default:
config.WindowConfig.left = WEBC_SKIN_CONFIRM_LEFT;
config.WindowConfig.top = WEBC_SKIN_CONFIRM_TOP;
config.WindowConfig.width = WEBC_SKIN_CONFIRM_WIDTH;
config.WindowConfig.height = WEBC_SKIN_CONFIRM_HEIGHT;
break;
}
config.WindowConfig.WindowFlags |= WF_MODAL;
HBROWSER_HANDLE Browser = webc_CreateBrowser(&config, WEBC_TRUE,"dialog", "");
if (Browser)
{
HDOC_HANDLE hDoc = webc_BrowserGetDocument(Browser);
wcCtx Ctx;
char buff[128];
wcCtxtInit(&Ctx, (wcBROW) Browser, (wcDOC) hDoc);
/* Experimental */
wcDocClear(&Ctx, wcFALSE);
wcDocOpen(&Ctx);
// Create a link between 'C' and Javascript the Native 'C' function PromptDismiss will
// be called with the value of the input field.
webc_JScriptDefineFunction(hDoc, "PromptDismiss", (JSNative) PromptDismiss, 0);
if (which_dialogue == WHICH_DIALOGUE_ALERT)
{
WriteDlgLine(&Ctx, WEBC_SKIN_ALERT_SOURCE_1);
WriteDlgLine(&Ctx, prompt_str);
WriteDlgLine(&Ctx, WEBC_SKIN_ALERT_SOURCE_2);
WriteDlgLine(&Ctx, WEBC_SKIN_ALERT_SOURCE_3);
}
else if (which_dialogue == WHICH_DIALOGUE_CONFIRM)
{
WriteDlgLine(&Ctx, WEBC_SKIN_CONFIRM_SOURCE_1);
WriteDlgLine(&Ctx, prompt_str);
WriteDlgLine(&Ctx, WEBC_SKIN_CONFIRM_SOURCE_2);
WriteDlgLine(&Ctx, WEBC_SKIN_CONFIRM_SOURCE_3);
}
else if (which_dialogue == WHICH_DIALOGUE_PROMPT)
{
char *default_str = "";
if (argc > 1)
default_str = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
WriteDlgLine(&Ctx, prompt_str);
rtp_sprintf(&buff[0],WEBC_SKIN_PROMPT_SOURCE_1,default_str);
WriteDlgLine(&Ctx, &buff[0]);
WriteDlgLine(&Ctx, WEBC_SKIN_PROMPT_SOURCE_2);
WriteDlgLine(&Ctx, WEBC_SKIN_PROMPT_SOURCE_3);
}
wcDocClose(&Ctx);
wcDocRefresh(&Ctx);
/* Go modal here */
// HBROWSER_HANDLE Browser = webc_CreateBrowser(&config, WEBC_TRUE, "");
HTMLBrowser *pBrowser = (HTMLBrowser *) Browser;
while (pBrowser->ProcessInputQueue(WEBC_TRUE) >= 0)
{
WebString *user_WebString;
void* privateData = webc_BrowserGetPrivateData(Browser);
if (privateData)
{
user_WebString = (WebString *) privateData;
if (which_dialogue == WHICH_DIALOGUE_PROMPT)
{
if (user_WebString && user_WebString->getLength())
*rval = STRING_TO_JSVAL(WEBC_JS_NEW_STRING_COPY_Z(cx, user_WebString->getChars()));
else
*rval = JSVAL_VOID;
//.........这里部分代码省略.........
示例2: js_AtomizeString
JSAtom *
js_AtomizeString(JSContext *cx, JSString *str, uintN flags)
{
jsval v;
JSAtomState *state;
JSDHashTable *table;
JSAtomHashEntry *entry;
JSString *key;
uint32 gen;
JS_ASSERT(!(flags & ~(ATOM_PINNED|ATOM_INTERNED|ATOM_TMPSTR|ATOM_NOCOPY)));
JS_ASSERT_IF(flags & ATOM_NOCOPY, flags & ATOM_TMPSTR);
if (str->isAtomized())
return (JSAtom *) STRING_TO_JSVAL(str);
size_t length = str->length();
if (length == 1) {
jschar c = str->chars()[0];
if (c < UNIT_STRING_LIMIT)
return (JSAtom *) STRING_TO_JSVAL(JSString::unitString(c));
}
/*
* Here we know that JSString::intStringTable covers only 256 (or at least
* not 1000 or more) chars. We rely on order here to resolve the unit vs.
* int string atom identity issue by giving priority to unit strings for
* '0' through '9' (see JSString::intString in jsstrinlines.h).
*/
JS_STATIC_ASSERT(INT_STRING_LIMIT <= 999);
if (2 <= length && length <= 3) {
const jschar *chars = str->chars();
if ('1' <= chars[0] && chars[0] <= '9' &&
'0' <= chars[1] && chars[1] <= '9' &&
(length == 2 || ('0' <= chars[2] && chars[2] <= '9'))) {
jsint i = (chars[0] - '0') * 10 + chars[1] - '0';
if (length == 3)
i = i * 10 + chars[2] - '0';
if (jsuint(i) < INT_STRING_LIMIT)
return (JSAtom *) STRING_TO_JSVAL(JSString::intString(i));
}
}
state = &cx->runtime->atomState;
table = &state->stringAtoms;
JS_LOCK(cx, &state->lock);
entry = TO_ATOM_ENTRY(JS_DHashTableOperate(table, str, JS_DHASH_ADD));
if (!entry)
goto failed_hash_add;
if (entry->keyAndFlags != 0) {
key = (JSString *)ATOM_ENTRY_KEY(entry);
} else {
/*
* We created a new hashtable entry. Unless str is already allocated
* from the GC heap and flat, we have to release state->lock as
* string construction is a complex operation. For example, it can
* trigger GC which may rehash the table and make the entry invalid.
*/
++table->generation;
if (!(flags & ATOM_TMPSTR) && str->isFlat()) {
str->flatClearMutable();
key = str;
} else {
gen = table->generation;
JS_UNLOCK(cx, &state->lock);
if (flags & ATOM_TMPSTR) {
if (flags & ATOM_NOCOPY) {
key = js_NewString(cx, str->flatChars(), str->flatLength());
if (!key)
return NULL;
/* Finish handing off chars to the GC'ed key string. */
str->mChars = NULL;
} else {
key = js_NewStringCopyN(cx, str->flatChars(), str->flatLength());
if (!key)
return NULL;
}
} else {
JS_ASSERT(str->isDependent());
if (!js_UndependString(cx, str))
return NULL;
key = str;
}
JS_LOCK(cx, &state->lock);
if (table->generation == gen) {
JS_ASSERT(entry->keyAndFlags == 0);
} else {
entry = TO_ATOM_ENTRY(JS_DHashTableOperate(table, key,
JS_DHASH_ADD));
if (!entry)
goto failed_hash_add;
if (entry->keyAndFlags != 0) {
key = (JSString *)ATOM_ENTRY_KEY(entry);
goto finish;
//.........这里部分代码省略.........
示例3: jsd_GetValueProperty
JSDProperty*
jsd_GetValueProperty(JSDContext* jsdc, JSDValue* jsdval, JSString* name)
{
JSContext* cx = jsdc->dumbContext;
JSDProperty* jsdprop;
JSDProperty* iter = NULL;
JSObject* obj;
uintN attrs = 0;
JSBool found;
JSPropertyDesc pd;
const jschar * nameChars;
size_t nameLen;
jsval val, nameval;
jsid nameid;
JSCrossCompartmentCall *call = NULL;
if(!jsd_IsValueObject(jsdc, jsdval))
return NULL;
/* If we already have the prop, then return it */
while(NULL != (jsdprop = jsd_IterateProperties(jsdc, jsdval, &iter)))
{
JSString* propName = jsd_GetValueString(jsdc, jsdprop->name);
if(propName) {
intN result;
if (JS_CompareStrings(cx, propName, name, &result) && !result)
return jsdprop;
}
JSD_DropProperty(jsdc, jsdprop);
}
/* Not found in property list, look it up explicitly */
if(!(obj = JSVAL_TO_OBJECT(jsdval->val)))
return NULL;
if (!(nameChars = JS_GetStringCharsZAndLength(cx, name, &nameLen)))
return NULL;
JS_BeginRequest(cx);
call = JS_EnterCrossCompartmentCall(cx, obj);
if(!call) {
JS_EndRequest(cx);
return NULL;
}
JS_GetUCPropertyAttributes(cx, obj, nameChars, nameLen, &attrs, &found);
if (!found)
{
JS_LeaveCrossCompartmentCall(call);
JS_EndRequest(cx);
return NULL;
}
JS_ClearPendingException(cx);
if(!JS_GetUCProperty(cx, obj, nameChars, nameLen, &val))
{
if (JS_IsExceptionPending(cx))
{
if (!JS_GetPendingException(cx, &pd.value))
{
JS_LeaveCrossCompartmentCall(call);
JS_EndRequest(cx);
return NULL;
}
pd.flags = JSPD_EXCEPTION;
}
else
{
pd.flags = JSPD_ERROR;
pd.value = JSVAL_VOID;
}
}
else
{
pd.value = val;
}
JS_LeaveCrossCompartmentCall(call);
JS_EndRequest(cx);
nameval = STRING_TO_JSVAL(name);
if (!JS_ValueToId(cx, nameval, &nameid) ||
!JS_IdToValue(cx, nameid, &pd.id)) {
return NULL;
}
pd.slot = pd.spare = 0;
pd.alias = JSVAL_NULL;
pd.flags |= (attrs & JSPROP_ENUMERATE) ? JSPD_ENUMERATE : 0
| (attrs & JSPROP_READONLY) ? JSPD_READONLY : 0
| (attrs & JSPROP_PERMANENT) ? JSPD_PERMANENT : 0;
return _newProperty(jsdc, &pd, JSDPD_HINTED);
}
示例4: JS_BINDED_PROP_GET_IMPL
JS_BINDED_PROP_GET_IMPL(FakeAudio, src)
{
JSString* str = JS_NewStringCopyZ(cx, src.c_str());
vp.set(STRING_TO_JSVAL(str));
return JS_TRUE;
}
示例5: log_and_maybe_keep_exception
static JSBool
log_and_maybe_keep_exception(JSContext *context,
char **message_p,
gboolean keep)
{
jsval exc = JSVAL_VOID;
JSString *s;
char *message;
JSBool retval = JS_FALSE;
JS_BeginRequest(context);
if (message_p)
*message_p = NULL;
JS_AddRoot(context, &exc);
if (!JS_GetPendingException(context, &exc))
goto out;
JS_ClearPendingException(context);
s = JS_ValueToString(context, exc);
if (s == NULL) {
gjs_debug(GJS_DEBUG_ERROR,
"Failed to convert exception to string");
goto out; /* Exception should be thrown already */
}
if (!gjs_string_to_utf8(context, STRING_TO_JSVAL(s), &message)) {
gjs_debug(GJS_DEBUG_ERROR,
"Failed to convert exception string to UTF-8");
goto out; /* Error already set */
}
gjs_debug(GJS_DEBUG_ERROR,
"Exception was: %s",
message);
if (message_p) {
*message_p = message;
} else {
g_free(message);
}
gjs_log_exception_props(context, exc);
/* We clear above and then set it back so any exceptions
* from the logging process don't overwrite the original
*/
if (keep)
JS_SetPendingException(context, exc);
retval = JS_TRUE;
out:
JS_RemoveRoot(context, &exc);
JS_EndRequest(context);
return retval;
}
示例6: exn_toSource
/*
* Return a string that may eval to something similar to the original object.
*/
static JSBool
exn_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
jsval v;
JSString *name, *message, *filename, *lineno_as_str, *result;
int32 lineno;
size_t lineno_length, name_length, message_length, filename_length, length;
jschar *chars, *cp;
if (!OBJ_GET_PROPERTY(cx, obj, (jsid)cx->runtime->atomState.nameAtom, &v))
return JS_FALSE;
name = js_ValueToString(cx, v);
if (!name)
return JS_FALSE;
if (!JS_GetProperty(cx, obj, js_message_str, &v) ||
!(message = js_ValueToSource(cx, v))) {
return JS_FALSE;
}
if (!JS_GetProperty(cx, obj, js_filename_str, &v) ||
!(filename = js_ValueToSource(cx, v))) {
return JS_FALSE;
}
if (!JS_GetProperty(cx, obj, js_lineno_str, &v) ||
!js_ValueToInt32 (cx, v, &lineno)) {
return JS_FALSE;
}
if (lineno != 0) {
if (!(lineno_as_str = js_ValueToString(cx, v))) {
return JS_FALSE;
}
lineno_length = JSSTRING_LENGTH(lineno_as_str);
} else {
lineno_as_str = NULL;
lineno_length = 0;
}
/* Magic 8, for the characters in ``(new ())''. */
name_length = JSSTRING_LENGTH(name);
message_length = JSSTRING_LENGTH(message);
length = 8 + name_length + message_length;
filename_length = JSSTRING_LENGTH(filename);
if (filename_length != 0) {
/* append filename as ``, {filename}'' */
length += 2 + filename_length;
if (lineno_as_str) {
/* append lineno as ``, {lineno_as_str}'' */
length += 2 + lineno_length;
}
} else {
if (lineno_as_str) {
/*
* no filename, but have line number,
* need to append ``, "", {lineno_as_str}''
*/
length += 6 + lineno_length;
}
}
cp = chars = (jschar*) JS_malloc(cx, (length + 1) * sizeof(jschar));
if (!chars)
return JS_FALSE;
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
js_strncpy(cp, JSSTRING_CHARS(name), name_length);
cp += name_length;
*cp++ = '(';
if (message_length != 0) {
js_strncpy(cp, JSSTRING_CHARS(message), message_length);
cp += message_length;
}
if (filename_length != 0) {
/* append filename as ``, {filename}'' */
*cp++ = ','; *cp++ = ' ';
js_strncpy(cp, JSSTRING_CHARS(filename), filename_length);
cp += filename_length;
} else {
if (lineno_as_str) {
/*
* no filename, but have line number,
* need to append ``, "", {lineno_as_str}''
*/
*cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
}
}
if (lineno_as_str) {
/* append lineno as ``, {lineno_as_str}'' */
*cp++ = ','; *cp++ = ' ';
js_strncpy(cp, JSSTRING_CHARS(lineno_as_str), lineno_length);
cp += lineno_length;
}
//.........这里部分代码省略.........
示例7: charptr_to_jsval
jsval charptr_to_jsval( JSContext *cx, const char *str)
{
JSString *ret_obj = JS_NewStringCopyZ(cx, str);
return STRING_TO_JSVAL(ret_obj);
}
示例8: hangReportMutex
NS_IMETHODIMP
TelemetryImpl::GetChromeHangs(JSContext *cx, jsval *ret)
{
MutexAutoLock hangReportMutex(mHangReportsMutex);
JSObject *reportArray = JS_NewArrayObject(cx, 0, nullptr);
if (!reportArray) {
return NS_ERROR_FAILURE;
}
*ret = OBJECT_TO_JSVAL(reportArray);
// Each hang report is an object in the 'chromeHangs' array
for (size_t i = 0; i < mHangReports.Length(); ++i) {
JSObject *reportObj = JS_NewObject(cx, NULL, NULL, NULL);
if (!reportObj) {
return NS_ERROR_FAILURE;
}
jsval reportObjVal = OBJECT_TO_JSVAL(reportObj);
if (!JS_SetElement(cx, reportArray, i, &reportObjVal)) {
return NS_ERROR_FAILURE;
}
// Record the hang duration (expressed in seconds)
JSBool ok = JS_DefineProperty(cx, reportObj, "duration",
INT_TO_JSVAL(mHangReports[i].duration),
NULL, NULL, JSPROP_ENUMERATE);
if (!ok) {
return NS_ERROR_FAILURE;
}
// Represent call stack PCs as strings
// (JS can't represent all 64-bit integer values)
JSObject *pcArray = JS_NewArrayObject(cx, 0, nullptr);
if (!pcArray) {
return NS_ERROR_FAILURE;
}
ok = JS_DefineProperty(cx, reportObj, "stack", OBJECT_TO_JSVAL(pcArray),
NULL, NULL, JSPROP_ENUMERATE);
if (!ok) {
return NS_ERROR_FAILURE;
}
const uint32_t pcCount = mHangReports[i].callStack.Length();
for (size_t pcIndex = 0; pcIndex < pcCount; ++pcIndex) {
nsCAutoString pcString;
pcString.AppendPrintf("0x%p", mHangReports[i].callStack[pcIndex]);
JSString *str = JS_NewStringCopyZ(cx, pcString.get());
if (!str) {
return NS_ERROR_FAILURE;
}
jsval v = STRING_TO_JSVAL(str);
if (!JS_SetElement(cx, pcArray, pcIndex, &v)) {
return NS_ERROR_FAILURE;
}
}
// Record memory map info
JSObject *moduleArray = JS_NewArrayObject(cx, 0, nullptr);
if (!moduleArray) {
return NS_ERROR_FAILURE;
}
ok = JS_DefineProperty(cx, reportObj, "memoryMap",
OBJECT_TO_JSVAL(moduleArray),
NULL, NULL, JSPROP_ENUMERATE);
if (!ok) {
return NS_ERROR_FAILURE;
}
#if defined(MOZ_ENABLE_PROFILER_SPS)
const uint32_t moduleCount = mHangReports[i].moduleMap.GetSize();
for (size_t moduleIndex = 0; moduleIndex < moduleCount; ++moduleIndex) {
// Current module
const SharedLibrary &module =
mHangReports[i].moduleMap.GetEntry(moduleIndex);
JSObject *moduleInfoArray = JS_NewArrayObject(cx, 0, nullptr);
if (!moduleInfoArray) {
return NS_ERROR_FAILURE;
}
jsval val = OBJECT_TO_JSVAL(moduleInfoArray);
if (!JS_SetElement(cx, moduleArray, moduleIndex, &val)) {
return NS_ERROR_FAILURE;
}
// Start address
nsCAutoString addressString;
addressString.AppendPrintf("0x%p", module.GetStart());
JSString *str = JS_NewStringCopyZ(cx, addressString.get());
if (!str) {
return NS_ERROR_FAILURE;
}
val = STRING_TO_JSVAL(str);
if (!JS_SetElement(cx, moduleInfoArray, 0, &val)) {
return NS_ERROR_FAILURE;
}
// Module name
str = JS_NewStringCopyZ(cx, module.GetName());
if (!str) {
return NS_ERROR_FAILURE;
}
//.........这里部分代码省略.........
示例9: NS_FAILED
// static
XPCNativeInterface*
XPCNativeInterface::NewInstance(XPCCallContext& ccx,
nsIInterfaceInfo* aInfo)
{
static const PRUint16 MAX_LOCAL_MEMBER_COUNT = 16;
XPCNativeMember local_members[MAX_LOCAL_MEMBER_COUNT];
XPCNativeInterface* obj = nsnull;
XPCNativeMember* members = nsnull;
int i;
JSBool failed = JS_FALSE;
PRUint16 constCount;
PRUint16 methodCount;
PRUint16 totalCount;
PRUint16 realTotalCount = 0;
XPCNativeMember* cur;
JSString* str;
jsval name;
jsval interfaceName;
// XXX Investigate lazy init? This is a problem given the
// 'placement new' scheme - we need to at least know how big to make
// the object. We might do a scan of methods to determine needed size,
// then make our object, but avoid init'ing *any* members until asked?
// Find out how often we create these objects w/o really looking at
// (or using) the members.
PRBool canScript;
if(NS_FAILED(aInfo->IsScriptable(&canScript)) || !canScript)
return nsnull;
if(NS_FAILED(aInfo->GetMethodCount(&methodCount)) ||
NS_FAILED(aInfo->GetConstantCount(&constCount)))
return nsnull;
// If the interface does not have nsISupports in its inheritance chain
// then we know we can't reflect its methods. However, some interfaces that
// are used just to reflect constants are declared this way. We need to
// go ahead and build the thing. But, we'll ignore whatever methods it may
// have.
if(!nsXPConnect::IsISupportsDescendant(aInfo))
methodCount = 0;
totalCount = methodCount + constCount;
if(totalCount > MAX_LOCAL_MEMBER_COUNT)
{
members = new XPCNativeMember[totalCount];
if(!members)
return nsnull;
}
else
{
members = local_members;
}
// NOTE: since getters and setters share a member, we might not use all
// of the member objects.
for(i = 0; i < methodCount; i++)
{
const nsXPTMethodInfo* info;
if(NS_FAILED(aInfo->GetMethodInfo(i, &info)))
{
failed = JS_TRUE;
break;
}
// don't reflect Addref or Release
if(i == 1 || i == 2)
continue;
if(!XPCConvert::IsMethodReflectable(*info))
continue;
str = JS_InternString(ccx, info->GetName());
if(!str)
{
NS_ASSERTION(0,"bad method name");
failed = JS_TRUE;
break;
}
name = STRING_TO_JSVAL(str);
if(info->IsSetter())
{
NS_ASSERTION(realTotalCount,"bad setter");
// Note: ASSUMES Getter/Setter pairs are next to each other
// This is a rule of the typelib spec.
cur = &members[realTotalCount-1];
NS_ASSERTION(cur->GetName() == name,"bad setter");
NS_ASSERTION(cur->IsReadOnlyAttribute(),"bad setter");
NS_ASSERTION(cur->GetIndex() == i-1,"bad setter");
cur->SetWritableAttribute();
}
else
{
// XXX need better way to find dups
// NS_ASSERTION(!LookupMemberByID(name),"duplicate method name");
//.........这里部分代码省略.........
示例10: DefineOSFileConstants
/**
* Define OS-specific constants.
*
* This function creates or uses JS object |OS.Constants| to store
* all its constants.
*/
bool DefineOSFileConstants(JSContext *cx, JS::Handle<JSObject*> global)
{
MOZ_ASSERT(gInitialized);
if (gPaths == nullptr) {
// If an initialization error was ignored, we may end up with
// |gInitialized == true| but |gPaths == nullptr|. We cannot
// |MOZ_ASSERT| this, as this would kill precompile_cache.js,
// so we simply return an error.
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
JSMSG_CANT_OPEN, "OSFileConstants", "initialization has failed");
return false;
}
JS::Rooted<JSObject*> objOS(cx);
if (!(objOS = GetOrCreateObjectProperty(cx, global, "OS"))) {
return false;
}
JS::Rooted<JSObject*> objConstants(cx);
if (!(objConstants = GetOrCreateObjectProperty(cx, objOS, "Constants"))) {
return false;
}
// Build OS.Constants.libc
JS::Rooted<JSObject*> objLibc(cx);
if (!(objLibc = GetOrCreateObjectProperty(cx, objConstants, "libc"))) {
return false;
}
if (!dom::DefineConstants(cx, objLibc, gLibcProperties)) {
return false;
}
#if defined(XP_WIN)
// Build OS.Constants.Win
JS::Rooted<JSObject*> objWin(cx);
if (!(objWin = GetOrCreateObjectProperty(cx, objConstants, "Win"))) {
return false;
}
if (!dom::DefineConstants(cx, objWin, gWinProperties)) {
return false;
}
#endif // defined(XP_WIN)
// Build OS.Constants.Sys
JS::Rooted<JSObject*> objSys(cx);
if (!(objSys = GetOrCreateObjectProperty(cx, objConstants, "Sys"))) {
return false;
}
nsCOMPtr<nsIXULRuntime> runtime = do_GetService(XULRUNTIME_SERVICE_CONTRACTID);
if (runtime) {
nsAutoCString os;
DebugOnly<nsresult> rv = runtime->GetOS(os);
MOZ_ASSERT(NS_SUCCEEDED(rv));
JSString* strVersion = JS_NewStringCopyZ(cx, os.get());
if (!strVersion) {
return false;
}
JS::Rooted<JS::Value> valVersion(cx, STRING_TO_JSVAL(strVersion));
if (!JS_SetProperty(cx, objSys, "Name", valVersion)) {
return false;
}
}
#if defined(DEBUG)
JS::Rooted<JS::Value> valDebug(cx, JSVAL_TRUE);
if (!JS_SetProperty(cx, objSys, "DEBUG", valDebug)) {
return false;
}
#endif
dom::ConstantSpec umask_cs[] = {
{ "umask", UINT_TO_JSVAL(gUserUmask) },
PROP_END
};
if (!dom::DefineConstants(cx, objSys, umask_cs)) {
return false;
}
// Build OS.Constants.Path
JS::Rooted<JSObject*> objPath(cx);
if (!(objPath = GetOrCreateObjectProperty(cx, objConstants, "Path"))) {
return false;
}
// Locate libxul
// Note that we don't actually provide the full path, only the name of the
// library, which is sufficient to link to the library using js-ctypes.
//.........这里部分代码省略.........
示例11: vreport_java_error
/*
* This is a wrapper around JS_ReportError(), useful when an error condition
* is the result of a JVM failure or exception condition. It appends the
* message associated with the pending Java exception to the passed in
* printf-style format string and arguments.
*/
static void
vreport_java_error(JSContext *cx, JNIEnv *jEnv, const char *format, va_list ap)
{
jobject java_obj;
jclass java_class;
JavaClassDescriptor *class_descriptor;
jthrowable java_exception;
JSType wrapped_exception_type;
jsval js_exception;
java_obj = NULL;
class_descriptor = NULL;
/* Get the exception out of the java environment. */
java_exception = (*jEnv)->ExceptionOccurred(jEnv);
if (!java_exception) {
JSString *err_jsstr;
char *err = JS_vsmprintf(format, ap);
if (!err)
return;
err_jsstr = JS_NewString(cx, err, strlen(err));
if (!err_jsstr)
return;
JS_SetPendingException(cx, STRING_TO_JSVAL(err_jsstr));
return;
}
(*jEnv)->ExceptionClear(jEnv);
/* Check for JSException */
if (njJSException &&
(*jEnv)->IsInstanceOf(jEnv, java_exception, njJSException)) {
wrapped_exception_type =
(*jEnv)->GetIntField(jEnv, java_exception,
njJSException_wrappedExceptionType);
/* (int) to suppress warning */
if ((int)wrapped_exception_type != JSTYPE_EMPTY) {
java_obj =
(*jEnv)->GetObjectField(jEnv, java_exception,
njJSException_wrappedException);
if ((java_obj == NULL) &&
(wrapped_exception_type == JSTYPE_OBJECT)) {
js_exception = JSVAL_NULL;
} else {
java_class = (*jEnv)->GetObjectClass(jEnv, java_obj);
class_descriptor = jsj_GetJavaClassDescriptor(cx, jEnv, java_class);
/* OK to delete ref, since above call adds global ref */
(*jEnv)->DeleteLocalRef(jEnv, java_class);
/* Convert native JS values back to native types. */
switch(wrapped_exception_type) {
case JSTYPE_NUMBER:
if (!jsj_ConvertJavaObjectToJSNumber(cx, jEnv,
class_descriptor,
java_obj,
&js_exception))
goto error;
break;
case JSTYPE_BOOLEAN:
if (!jsj_ConvertJavaObjectToJSBoolean(cx, jEnv,
class_descriptor,
java_obj,
&js_exception))
goto error;
break;
case JSTYPE_STRING:
if (!jsj_ConvertJavaObjectToJSString(cx, jEnv,
class_descriptor,
java_obj,
&js_exception))
goto error;
break;
case JSTYPE_VOID:
js_exception = JSVAL_VOID;
break;
case JSTYPE_OBJECT:
case JSTYPE_FUNCTION:
default:
if ((*jEnv)->IsInstanceOf(jEnv, java_obj, njJSObject)) {
js_exception = OBJECT_TO_JSVAL(jsj_UnwrapJSObjectWrapper(jEnv, java_obj));
if (!js_exception)
goto error;
} else {
if (!jsj_ConvertJavaObjectToJSValue(cx, jEnv, java_obj,
&js_exception))
goto error;
}
}
}
}
//.........这里部分代码省略.........
示例12: js_destroy
JSBool js_destroy(JSContext *context) {
JS_SetPendingException(context, STRING_TO_JSVAL(JS_NewStringCopyZ(context, "timeout")));
return JS_FALSE;
}
示例13: js_console_get
static JSBool js_console_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
ulong val;
jsint tiny;
JSString* js_str;
sbbs_t* sbbs;
if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL)
return(JS_FALSE);
tiny = JSVAL_TO_INT(id);
switch(tiny) {
case CON_PROP_STATUS:
val=sbbs->console;
break;
case CON_PROP_LNCNTR:
val=sbbs->lncntr;
break;
case CON_PROP_ATTR:
val=sbbs->curatr;
break;
case CON_PROP_TOS:
val=sbbs->tos;
break;
case CON_PROP_ROWS:
val=sbbs->rows;
break;
case CON_PROP_COLUMNS:
val=sbbs->cols;
break;
case CON_PROP_AUTOTERM:
val=sbbs->autoterm;
break;
case CON_PROP_TERMINAL:
if((js_str=JS_NewStringCopyZ(cx, sbbs->terminal))==NULL)
return(JS_FALSE);
*vp = STRING_TO_JSVAL(js_str);
return(JS_TRUE);
case CON_PROP_TIMEOUT:
val=sbbs->timeout;
break;
case CON_PROP_TIMELEFT_WARN:
val=sbbs->timeleft_warn;
break;
case CON_PROP_ABORTED:
*vp=BOOLEAN_TO_JSVAL(INT_TO_BOOL(sbbs->sys_status&SS_ABORT));
return(JS_TRUE);
case CON_PROP_ABORTABLE:
*vp=BOOLEAN_TO_JSVAL(INT_TO_BOOL(sbbs->rio_abortable));
return(JS_TRUE);
case CON_PROP_TELNET_MODE:
val=sbbs->telnet_mode;
break;
case CON_PROP_GETSTR_OFFSET:
val=sbbs->getstr_offset;
break;
case CON_PROP_WORDWRAP:
if((js_str=JS_NewStringCopyZ(cx, sbbs->wordwrap))==NULL)
return(JS_FALSE);
*vp = STRING_TO_JSVAL(js_str);
return(JS_TRUE);
case CON_PROP_QUESTION:
if((js_str=JS_NewStringCopyZ(cx, sbbs->question))==NULL)
return(JS_FALSE);
*vp = STRING_TO_JSVAL(js_str);
return(JS_TRUE);
case CON_PROP_CTRLKEY_PASSTHRU:
val=sbbs->cfg.ctrlkey_passthru;
break;
case CON_PROP_INBUF_LEVEL:
val=RingBufFull(&sbbs->inbuf);
break;
case CON_PROP_INBUF_SPACE:
val=RingBufFree(&sbbs->inbuf);
break;
case CON_PROP_OUTBUF_LEVEL:
val=RingBufFull(&sbbs->outbuf);
break;
case CON_PROP_OUTBUF_SPACE:
val=RingBufFree(&sbbs->outbuf);
break;
default:
return(JS_TRUE);
}
*vp = INT_TO_JSVAL(val);
return(JS_TRUE);
}
示例14: js_AtomizeString
JSAtom *
js_AtomizeString(JSContext *cx, JSString *str, uintN flags)
{
JSHashNumber keyHash;
jsval key;
JSAtomState *state;
JSHashTable *table;
JSHashEntry *he, **hep;
JSAtom *atom;
keyHash = js_HashString(str);
key = STRING_TO_JSVAL(str);
state = &cx->runtime->atomState;
JS_LOCK(&state->lock, cx);
table = state->table;
hep = JS_HashTableRawLookup(table, keyHash, (void *)key);
if ((he = *hep) == NULL) {
#ifdef JS_THREADSAFE
uint32 gen = state->tablegen;
JS_UNLOCK(&state->lock, cx);
#endif
if (flags & ATOM_TMPSTR) {
str = (flags & ATOM_NOCOPY)
? js_NewString(cx, str->chars, str->length, 0)
: js_NewStringCopyN(cx, str->chars, str->length, 0);
if (!str)
return NULL;
key = STRING_TO_JSVAL(str);
} else {
if (!JS_MakeStringImmutable(cx, str))
return NULL;
}
#ifdef JS_THREADSAFE
JS_LOCK(&state->lock, cx);
if (state->tablegen != gen) {
hep = JS_HashTableRawLookup(table, keyHash, (void *)key);
if ((he = *hep) != NULL) {
atom = (JSAtom *)he;
if (flags & ATOM_NOCOPY)
str->chars = NULL;
goto out;
}
}
#endif
he = JS_HashTableRawAdd(table, hep, keyHash, (void *)key, NULL);
if (!he) {
JS_ReportOutOfMemory(cx);
atom = NULL;
goto out;
}
}
atom = (JSAtom *)he;
atom->flags |= flags & (ATOM_PINNED | ATOM_INTERNED);
out:
JS_UNLOCK(&state->lock,cx);
return atom;
}
示例15: Window_getString
JSBool
Window_getString (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
JSObject* options;
WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);
JS_BeginRequest(cx);
JS_EnterLocalRootScope(cx);
jsval parent;
JS_GetProperty(cx, JS_GetGlobalObject(cx), "ncurses", &parent);
JS_GetProperty(cx, JSVAL_TO_OBJECT(parent), "Screen", &parent);
JSObject* Screen = JSVAL_TO_OBJECT(parent);
jsval property;
JS_GetProperty(cx, Screen, "echo", &property);
JSBool windowEchoing = JSVAL_TO_BOOLEAN(property);
JS_GetProperty(cx, Screen, "cursor", &property);
jsint windowCursor = JSVAL_TO_INT(property);
if (argc == 0) {
echo();
curs_set(1);
*rval = STRING_TO_JSVAL(__Window_readLine(cx, win, JS_FALSE, 0, 0));
if (!windowEchoing) {
noecho();
}
curs_set(windowCursor);
}
else {
JS_ValueToObject(cx, argv[0], &options);
if (!options) {
JS_ReportError(cx, "Options isn't a valid object.");
JS_LeaveLocalRootScope(cx);
JS_EndRequest(cx);
return JS_FALSE;
}
jsval x, y, jsEcho;
JS_GetProperty(cx, options, "x", &x);
JS_GetProperty(cx, options, "y", &y);
JS_GetProperty(cx, options, "echo", &jsEcho);
if (!JSVAL_IS_INT(x) || !JSVAL_IS_INT(y)) {
__Window_echofy(jsEcho, windowEchoing, windowCursor, JS_TRUE);
*rval = STRING_TO_JSVAL(__Window_readLine(cx, win, JS_FALSE, 0, 0));
__Window_echofy(jsEcho, windowEchoing, windowCursor, JS_FALSE);
}
else {
__Window_echofy(jsEcho, windowEchoing, windowCursor, JS_TRUE);
*rval = STRING_TO_JSVAL(__Window_readLine(cx, win, JS_TRUE, x, y));
__Window_echofy(jsEcho, windowEchoing, windowCursor, JS_FALSE);
}
}
JS_LeaveLocalRootScope(cx);
JS_EndRequest(cx);
return JS_TRUE;
}