本文整理汇总了C++中JSAutoByteString::ptr方法的典型用法代码示例。如果您正苦于以下问题:C++ JSAutoByteString::ptr方法的具体用法?C++ JSAutoByteString::ptr怎么用?C++ JSAutoByteString::ptr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSAutoByteString
的用法示例。
在下文中一共展示了JSAutoByteString::ptr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: id
/* |callee| requires a usage string provided by JS_DefineFunctionsWithHelp. */
void
js::ReportUsageErrorASCII(JSContext* cx, HandleObject callee, const char* msg)
{
const char* usageStr = "usage";
PropertyName* usageAtom = Atomize(cx, usageStr, strlen(usageStr))->asPropertyName();
RootedId id(cx, NameToId(usageAtom));
DebugOnly<Shape*> shape = static_cast<Shape*>(callee->as<JSFunction>().lookup(cx, id));
MOZ_ASSERT(!shape->configurable());
MOZ_ASSERT(!shape->writable());
MOZ_ASSERT(shape->hasDefaultGetter());
RootedValue usage(cx);
if (!JS_GetProperty(cx, callee, "usage", &usage))
return;
if (!usage.isString()) {
JS_ReportErrorASCII(cx, "%s", msg);
} else {
RootedString usageStr(cx, usage.toString());
JSAutoByteString str;
if (!str.encodeUtf8(cx, usageStr))
return;
JS_ReportErrorUTF8(cx, "%s. Usage: %s", msg, str.ptr());
}
}
示例2: script
static bool
ProcessLine(AutoJSAPI& jsapi, const char* buffer, int startline)
{
JSContext* cx = jsapi.cx();
JS::RootedScript script(cx);
JS::RootedValue result(cx);
JS::CompileOptions options(cx);
options.setFileAndLine("typein", startline)
.setIsRunOnce(true);
if (!JS_CompileScript(cx, buffer, strlen(buffer), options, &script))
return false;
if (compileOnly)
return true;
if (!JS_ExecuteScript(cx, script, &result))
return false;
if (result.isUndefined())
return true;
RootedString str(cx);
if (!(str = ToString(cx, result)))
return false;
JSAutoByteString bytes;
if (!bytes.encodeLatin1(cx, str))
return false;
fprintf(gOutFile, "%s\n", bytes.ptr());
return true;
}
示例3: fopen
static JSBool
DumpHeapComplete(JSContext *cx, unsigned argc, jsval *vp)
{
const char *fileName = NULL;
JSAutoByteString fileNameBytes;
if (argc > 0) {
Value v = JS_ARGV(cx, vp)[0];
if (v.isString()) {
JSString *str = v.toString();
if (!fileNameBytes.encodeLatin1(cx, str))
return false;
fileName = fileNameBytes.ptr();
}
}
FILE *dumpFile;
if (!fileName) {
dumpFile = stdout;
} else {
dumpFile = fopen(fileName, "w");
if (!dumpFile) {
JS_ReportError(cx, "can't open %s", fileName);
return false;
}
}
js::DumpHeapComplete(JS_GetRuntime(cx), dumpFile);
fclose(dumpFile);
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return true;
}
示例4: v
static inline void
ReportInvalidTrapResult(JSContext* cx, JSObject* proxy, JSAtom* atom)
{
RootedValue v(cx, ObjectOrNullValue(proxy));
JSAutoByteString bytes;
if (!AtomToPrintableString(cx, atom, &bytes))
return;
ReportValueError2(cx, JSMSG_INVALID_TRAP_RESULT, JSDVG_IGNORE_STACK, v,
nullptr, bytes.ptr());
}
示例5: CallArgsFromVp
static bool
DumpHeapComplete(JSContext *cx, unsigned argc, jsval *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
DumpHeapNurseryBehaviour nurseryBehaviour = js::IgnoreNurseryObjects;
FILE *dumpFile = nullptr;
unsigned i = 0;
if (argc > i) {
Value v = args[i];
if (v.isString()) {
JSString *str = v.toString();
bool same = false;
if (!JS_StringEqualsAscii(cx, str, "collectNurseryBeforeDump", &same))
return false;
if (same) {
nurseryBehaviour = js::CollectNurseryBeforeDump;
++i;
}
}
}
if (argc > i) {
Value v = args[i];
if (v.isString()) {
if (!fuzzingSafe) {
JSString *str = v.toString();
JSAutoByteString fileNameBytes;
if (!fileNameBytes.encodeLatin1(cx, str))
return false;
const char *fileName = fileNameBytes.ptr();
dumpFile = fopen(fileName, "w");
if (!dumpFile) {
JS_ReportError(cx, "can't open %s", fileName);
return false;
}
}
++i;
}
}
if (i != argc) {
JS_ReportError(cx, "bad arguments passed to dumpHeapComplete");
return false;
}
js::DumpHeapComplete(JS_GetRuntime(cx), dumpFile ? dumpFile : stdout, nurseryBehaviour);
if (dumpFile)
fclose(dumpFile);
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return true;
}
示例6:
bool
ModuleBuilder::appendLocalExportEntry(HandleExportEntryObject exp)
{
if (!module_->initialEnvironment().lookup(cx_, AtomToId(exp->localName()))) {
JSAutoByteString str;
str.encodeLatin1(cx_, exp->localName());
JS_ReportErrorNumber(cx_, GetErrorMessage, nullptr, JSMSG_MISSING_EXPORT, str.ptr());
return false;
}
return localExportEntries_.append(exp);
}
示例7: ar
JSBool
xpc_DumpEvalInJSStackFrame(JSContext* cx, JSUint32 frameno, const char* text)
{
JSStackFrame* fp;
JSStackFrame* iter = nsnull;
JSUint32 num = 0;
if(!cx || !text)
{
puts("invalid params passed to xpc_DumpEvalInJSStackFrame!");
return JS_FALSE;
}
printf("js[%d]> %s\n", frameno, text);
while(nsnull != (fp = JS_FrameIterator(cx, &iter)))
{
if(num == frameno)
break;
num++;
}
if(!fp)
{
puts("invalid frame number!");
return JS_FALSE;
}
JSAutoRequest ar(cx);
JSExceptionState* exceptionState = JS_SaveExceptionState(cx);
JSErrorReporter older = JS_SetErrorReporter(cx, xpcDumpEvalErrorReporter);
jsval rval;
JSString* str;
JSAutoByteString bytes;
if(JS_EvaluateInStackFrame(cx, fp, text, strlen(text), "eval", 1, &rval) &&
nsnull != (str = JS_ValueToString(cx, rval)) &&
bytes.encode(cx, str))
{
printf("%s\n", bytes.ptr());
}
else
puts("eval failed!");
JS_SetErrorReporter(cx, older);
JS_RestoreExceptionState(cx, exceptionState);
return JS_TRUE;
}
示例8: iter
JSBool
xpc_DumpEvalInJSStackFrame(JSContext* cx, uint32_t frameno, const char* text)
{
if (!cx || !text) {
DebugDump("%s", "invalid params passed to xpc_DumpEvalInJSStackFrame!\n");
return false;
}
DebugDump("js[%d]> %s\n", frameno, text);
uint32_t num = 0;
JSAbstractFramePtr frame = JSNullFramePtr();
JSBrokenFrameIterator iter(cx);
while (!iter.done()) {
if (num == frameno) {
frame = iter.abstractFramePtr();
break;
}
++iter;
num++;
}
if (!frame) {
DebugDump("%s", "invalid frame number!\n");
return false;
}
JSAutoRequest ar(cx);
JSExceptionState* exceptionState = JS_SaveExceptionState(cx);
JSErrorReporter older = JS_SetErrorReporter(cx, xpcDumpEvalErrorReporter);
jsval rval;
JSString* str;
JSAutoByteString bytes;
if (frame.evaluateInStackFrame(cx, text, strlen(text), "eval", 1, &rval) &&
nullptr != (str = JS_ValueToString(cx, rval)) &&
bytes.encode(cx, str)) {
DebugDump("%s\n", bytes.ptr());
} else
DebugDump("%s", "eval failed!\n");
JS_SetErrorReporter(cx, older);
JS_RestoreExceptionState(cx, exceptionState);
return true;
}
示例9: usage
/* |callee| requires a usage string provided by JS_DefineFunctionsWithHelp. */
void
js::ReportUsageErrorASCII(JSContext* cx, HandleObject callee, const char* msg)
{
RootedValue usage(cx);
if (!JS_GetProperty(cx, callee, "usage", &usage))
return;
if (!usage.isString()) {
JS_ReportErrorASCII(cx, "%s", msg);
} else {
RootedString usageStr(cx, usage.toString());
JSAutoByteString str;
if (!str.encodeUtf8(cx, usageStr))
return;
JS_ReportErrorUTF8(cx, "%s. Usage: %s", msg, str.ptr());
}
}
示例10: message
JSErrorReport*
js::ErrorObject::getOrCreateErrorReport(JSContext* cx)
{
if (JSErrorReport* r = getErrorReport())
return r;
// We build an error report on the stack and then use CopyErrorReport to do
// the nitty-gritty malloc stuff.
JSErrorReport report;
// Type.
JSExnType type_ = type();
report.exnType = type_;
// Filename.
JSAutoByteString filenameStr;
if (!filenameStr.encodeLatin1(cx, fileName(cx)))
return nullptr;
report.filename = filenameStr.ptr();
// Coordinates.
report.lineno = lineNumber();
report.column = columnNumber();
// Message. Note that |new Error()| will result in an undefined |message|
// slot, so we need to explicitly substitute the empty string in that case.
RootedString message(cx, getMessage());
if (!message)
message = cx->runtime()->emptyString;
if (!message->ensureFlat(cx))
return nullptr;
UniquePtr<char[], JS::FreePolicy> utf8 = StringToNewUTF8CharsZ(cx, *message);
if (!utf8)
return nullptr;
report.initOwnedMessage(utf8.release());
// Cache and return.
JSErrorReport* copy = CopyErrorReport(cx, &report);
if (!copy)
return nullptr;
setReservedSlot(ERROR_REPORT_SLOT, PrivateValue(copy));
return copy;
}
示例11: JS_btoa
// {{{ Conversions
bool JSGlobal::JS_btoa(JSContext *cx, JS::CallArgs &args)
{
if (args[0].isString()) {
JSAutoByteString cdata;
JS::RootedString str(cx, args[0].toString());
cdata.encodeUtf8(cx, str);
char *ret = Utils::B64Encode(
reinterpret_cast<unsigned char *>(cdata.ptr()), cdata.length());
args.rval().setString(JS_NewStringCopyZ(cx, ret));
free(ret);
} else {
args.rval().setNull();
JS_ReportWarning(cx, "btoa() non-string given");
}
return true;
}
示例12: closeCalendar
static bool
DefaultCalendar(JSContext* cx, const JSAutoByteString& locale, MutableHandleValue rval)
{
UErrorCode status = U_ZERO_ERROR;
UCalendar* cal = ucal_open(nullptr, 0, locale.ptr(), UCAL_DEFAULT, &status);
// This correctly handles nullptr |cal| when opening failed.
ScopedICUObject<UCalendar, ucal_close> closeCalendar(cal);
const char* calendar = ucal_getType(cal, &status);
if (U_FAILURE(status)) {
intl::ReportInternalError(cx);
return false;
}
// ICU returns old-style keyword values; map them to BCP 47 equivalents
JSString* str = JS_NewStringCopyZ(cx, uloc_toUnicodeLocaleType("ca", calendar));
if (!str)
return false;
rval.setString(str);
return true;
}
示例13: if
void
XPCShellEnvironment::ProcessFile(JSContext *cx,
JS::Handle<JSObject*> obj,
const char *filename,
FILE *file,
bool forceTTY)
{
XPCShellEnvironment* env = this;
JS::Rooted<JS::Value> result(cx);
int lineno, startline;
bool ok, hitEOF;
char *bufp, buffer[4096];
JSString *str;
if (forceTTY) {
file = stdin;
}
else if (!isatty(fileno(file)))
{
/*
* It's not interactive - just execute it.
*
* Support the UNIX #! shell hack; gobble the first line if it starts
* with '#'. TODO - this isn't quite compatible with sharp variables,
* as a legal js program (using sharp variables) might start with '#'.
* But that would require multi-character lookahead.
*/
int ch = fgetc(file);
if (ch == '#') {
while((ch = fgetc(file)) != EOF) {
if(ch == '\n' || ch == '\r')
break;
}
}
ungetc(ch, file);
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, obj);
JS::CompileOptions options(cx);
options.setUTF8(true)
.setFileAndLine(filename, 1);
JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
if (script)
(void)JS_ExecuteScript(cx, obj, script, result.address());
return;
}
/* It's an interactive filehandle; drop into read-eval-print loop. */
lineno = 1;
hitEOF = false;
do {
bufp = buffer;
*bufp = '\0';
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, obj);
/*
* Accumulate lines until we get a 'compilable unit' - one that either
* generates an error (before running out of source) or that compiles
* cleanly. This should be whenever we get a complete statement that
* coincides with the end of a line.
*/
startline = lineno;
do {
if (!GetLine(bufp, file, startline == lineno ? "js> " : "")) {
hitEOF = true;
break;
}
bufp += strlen(bufp);
lineno++;
} while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
/* Clear any pending exception from previous failed compiles. */
JS_ClearPendingException(cx);
JS::CompileOptions options(cx);
options.setFileAndLine("typein", startline);
JS::Rooted<JSScript*> script(cx,
JS_CompileScript(cx, obj, buffer, strlen(buffer), options));
if (script) {
JSErrorReporter older;
ok = JS_ExecuteScript(cx, obj, script, result.address());
if (ok && result != JSVAL_VOID) {
/* Suppress error reports from JS::ToString(). */
older = JS_SetErrorReporter(cx, nullptr);
str = JS::ToString(cx, result);
JSAutoByteString bytes;
if (str)
bytes.encodeLatin1(cx, str);
JS_SetErrorReporter(cx, older);
if (!!bytes)
fprintf(stdout, "%s\n", bytes.ptr());
else
ok = false;
}
//.........这里部分代码省略.........
示例14: fun
void JS_FASTCALL
stubs::DefFun(VMFrame &f, JSFunction *fun_)
{
/*
* A top-level function defined in Global or Eval code (see ECMA-262
* Ed. 3), or else a SpiderMonkey extension: a named function statement in
* a compound statement (not at the top statement level of global code, or
* at the top level of a function body).
*/
JSContext *cx = f.cx;
StackFrame *fp = f.fp();
RootedFunction fun(f.cx, fun_);
/*
* If static link is not current scope, clone fun's object to link to the
* current scope via parent. We do this to enable sharing of compiled
* functions among multiple equivalent scopes, amortizing the cost of
* compilation over a number of executions. Examples include XUL scripts
* and event handlers shared among Firefox or other Mozilla app chrome
* windows, and user-defined JS functions precompiled and then shared among
* requests in server-side JS.
*/
HandleObject scopeChain = f.fp()->scopeChain();
if (fun->environment() != scopeChain) {
fun = CloneFunctionObjectIfNotSingleton(cx, fun, scopeChain);
if (!fun)
THROW();
} else {
JS_ASSERT(f.script()->compileAndGo);
JS_ASSERT(f.fp()->isGlobalFrame() || f.fp()->isEvalInFunction());
}
/*
* ECMA requires functions defined when entering Eval code to be
* impermanent.
*/
unsigned attrs = fp->isEvalFrame()
? JSPROP_ENUMERATE
: JSPROP_ENUMERATE | JSPROP_PERMANENT;
/*
* We define the function as a property of the variable object and not the
* current scope chain even for the case of function expression statements
* and functions defined by eval inside let or with blocks.
*/
JSObject *parent = &fp->varObj();
/* ES5 10.5 (NB: with subsequent errata). */
PropertyName *name = fun->atom->asPropertyName();
JSProperty *prop = NULL;
JSObject *pobj;
if (!parent->lookupProperty(cx, name, &pobj, &prop))
THROW();
Value rval = ObjectValue(*fun);
do {
/* Steps 5d, 5f. */
if (!prop || pobj != parent) {
if (!parent->defineProperty(cx, name, rval,
JS_PropertyStub, JS_StrictPropertyStub, attrs))
{
THROW();
}
break;
}
/* Step 5e. */
JS_ASSERT(parent->isNative());
Shape *shape = reinterpret_cast<Shape *>(prop);
if (parent->isGlobal()) {
if (shape->configurable()) {
if (!parent->defineProperty(cx, name, rval,
JS_PropertyStub, JS_StrictPropertyStub, attrs))
{
THROW();
}
break;
}
if (shape->isAccessorDescriptor() || !shape->writable() || !shape->enumerable()) {
JSAutoByteString bytes;
if (js_AtomToPrintableString(cx, name, &bytes)) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_CANT_REDEFINE_PROP, bytes.ptr());
}
THROW();
}
}
/*
* Non-global properties, and global properties which we aren't simply
* redefining, must be set. First, this preserves their attributes.
* Second, this will produce warnings and/or errors as necessary if the
* specified Call object property is not writable (const).
*/
/* Step 5f. */
if (!parent->setProperty(cx, name, &rval, strict))
THROW();
//.........这里部分代码省略.........
示例15: tvr
JSBool
js_ReportUncaughtException(JSContext *cx)
{
jsval exn;
JSObject *exnObject;
jsval roots[5];
JSErrorReport *reportp, report;
JSString *str;
const char *bytes;
if (!JS_IsExceptionPending(cx))
return true;
if (!JS_GetPendingException(cx, &exn))
return false;
PodArrayZero(roots);
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), Valueify(roots));
/*
* Because js_ValueToString below could error and an exception object
* could become unrooted, we must root exnObject. Later, if exnObject is
* non-null, we need to root other intermediates, so allocate an operand
* stack segment to protect all of these values.
*/
if (JSVAL_IS_PRIMITIVE(exn)) {
exnObject = NULL;
} else {
exnObject = JSVAL_TO_OBJECT(exn);
roots[0] = exn;
}
JS_ClearPendingException(cx);
reportp = js_ErrorFromException(cx, exn);
/* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
str = js_ValueToString(cx, Valueify(exn));
JSAutoByteString bytesStorage;
if (!str) {
bytes = "unknown (can't convert to string)";
} else {
roots[1] = STRING_TO_JSVAL(str);
if (!bytesStorage.encode(cx, str))
return false;
bytes = bytesStorage.ptr();
}
JSAutoByteString filename;
if (!reportp && exnObject && exnObject->getClass() == &js_ErrorClass) {
if (!JS_GetProperty(cx, exnObject, js_message_str, &roots[2]))
return false;
if (JSVAL_IS_STRING(roots[2])) {
bytesStorage.clear();
if (!bytesStorage.encode(cx, str))
return false;
bytes = bytesStorage.ptr();
}
if (!JS_GetProperty(cx, exnObject, js_fileName_str, &roots[3]))
return false;
str = js_ValueToString(cx, Valueify(roots[3]));
if (!str || !filename.encode(cx, str))
return false;
if (!JS_GetProperty(cx, exnObject, js_lineNumber_str, &roots[4]))
return false;
uint32_t lineno;
if (!ValueToECMAUint32(cx, Valueify(roots[4]), &lineno))
return false;
reportp = &report;
PodZero(&report);
report.filename = filename.ptr();
report.lineno = (uintN) lineno;
if (JSVAL_IS_STRING(roots[2])) {
JSFixedString *fixed = JSVAL_TO_STRING(roots[2])->ensureFixed(cx);
if (!fixed)
return false;
report.ucmessage = fixed->chars();
}
}
if (!reportp) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_UNCAUGHT_EXCEPTION, bytes);
} else {
/* Flag the error as an exception. */
reportp->flags |= JSREPORT_EXCEPTION;
/* Pass the exception object. */
JS_SetPendingException(cx, exn);
js_ReportErrorAgain(cx, bytes, reportp);
JS_ClearPendingException(cx);
}
return true;
}