summaryrefslogtreecommitdiff
path: root/src/couch/priv/couch_js/60/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/couch/priv/couch_js/60/main.cpp')
-rw-r--r--src/couch/priv/couch_js/60/main.cpp70
1 files changed, 52 insertions, 18 deletions
diff --git a/src/couch/priv/couch_js/60/main.cpp b/src/couch/priv/couch_js/60/main.cpp
index e78dbb46d..828b9dab5 100644
--- a/src/couch/priv/couch_js/60/main.cpp
+++ b/src/couch/priv/couch_js/60/main.cpp
@@ -15,6 +15,7 @@
#include <string.h>
#ifdef XP_WIN
+#define NOMINMAX
#include <windows.h>
#else
#include <unistd.h>
@@ -27,7 +28,6 @@
#include "config.h"
#include "http.h"
-#include "utf8.h"
#include "util.h"
static bool enableSharedMemory = true;
@@ -98,8 +98,9 @@ req_ctor(JSContext* cx, unsigned int argc, JS::Value* vp)
static bool
req_open(JSContext* cx, unsigned int argc, JS::Value* vp)
{
- JSObject* obj = JS_THIS_OBJECT(cx, vp);
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+ JS::Value vobj = args.computeThis(cx);
+ JSObject* obj = vobj.toObjectOrNull();
bool ret = false;
if(argc == 2) {
@@ -118,8 +119,9 @@ req_open(JSContext* cx, unsigned int argc, JS::Value* vp)
static bool
req_set_hdr(JSContext* cx, unsigned int argc, JS::Value* vp)
{
- JSObject* obj = JS_THIS_OBJECT(cx, vp);
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+ JS::Value vobj = args.computeThis(cx);
+ JSObject* obj = vobj.toObjectOrNull();
bool ret = false;
if(argc == 2) {
@@ -136,8 +138,9 @@ req_set_hdr(JSContext* cx, unsigned int argc, JS::Value* vp)
static bool
req_send(JSContext* cx, unsigned int argc, JS::Value* vp)
{
- JSObject* obj = JS_THIS_OBJECT(cx, vp);
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+ JS::Value vobj = args.computeThis(cx);
+ JSObject* obj = vobj.toObjectOrNull();
bool ret = false;
if(argc == 1) {
@@ -154,7 +157,9 @@ static bool
req_status(JSContext* cx, unsigned int argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
- JSObject* obj = JS_THIS_OBJECT(cx, vp);
+ JS::Value vobj = args.computeThis(cx);
+ JSObject* obj = vobj.toObjectOrNull();
+
int status = http_status(cx, obj);
if(status < 0)
@@ -168,8 +173,10 @@ static bool
base_url(JSContext *cx, unsigned int argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
- JSObject* obj = JS_THIS_OBJECT(cx, vp);
- couch_args *cargs = (couch_args*)JS_GetContextPrivate(cx);
+ JS::Value vobj = args.computeThis(cx);
+ JSObject* obj = vobj.toObjectOrNull();
+
+ couch_args *cargs = static_cast<couch_args*>(JS_GetContextPrivate(cx));
JS::Value uri_val;
bool rc = http_uri(cx, obj, cargs, &uri_val);
args.rval().set(uri_val);
@@ -225,9 +232,15 @@ evalcx(JSContext *cx, unsigned int argc, JS::Value* vp)
if (!sandbox)
return false;
}
- JS_BeginRequest(cx);
+
JSAutoRequest ar(cx);
+ if (!sandbox) {
+ sandbox = NewSandbox(cx, false);
+ if (!sandbox)
+ return false;
+ }
+
js::AutoStableStringChars strChars(cx);
if (!strChars.initTwoByte(cx, str))
return false;
@@ -236,12 +249,6 @@ evalcx(JSContext *cx, unsigned int argc, JS::Value* vp)
size_t srclen = chars.length();
const char16_t* src = chars.begin().get();
- if (!sandbox) {
- sandbox = NewSandbox(cx, false);
- if (!sandbox)
- return false;
- }
-
if(srclen == 0) {
args.rval().setObject(*sandbox);
} else {
@@ -282,7 +289,19 @@ static bool
print(JSContext* cx, unsigned int argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
- couch_print(cx, argc, args);
+
+ bool use_stderr = false;
+ if(argc > 1 && args[1].isTrue()) {
+ use_stderr = true;
+ }
+
+ if(!args[0].isString()) {
+ JS_ReportErrorUTF8(cx, "Unable to print non-string value.");
+ return false;
+ }
+
+ couch_print(cx, args[0], use_stderr);
+
args.rval().setUndefined();
return true;
}
@@ -385,7 +404,7 @@ static JSFunctionSpec global_functions[] = {
static bool
csp_allows(JSContext* cx)
{
- couch_args *args = (couch_args*)JS_GetContextPrivate(cx);
+ couch_args* args = static_cast<couch_args*>(JS_GetContextPrivate(cx));
if(args->eval) {
return true;
} else {
@@ -472,10 +491,18 @@ main(int argc, const char* argv[])
// Compile and run
JS::CompileOptions options(cx);
options.setFileAndLine(args->scripts[i], 1);
+ options.setUTF8(true);
JS::RootedScript script(cx);
if(!JS_CompileScript(cx, scriptsrc, slen, options, &script)) {
- fprintf(stderr, "Failed to compile script.\n");
+ JS::RootedValue exc(cx);
+ if(!JS_GetPendingException(cx, &exc)) {
+ fprintf(stderr, "Failed to compile script.\n");
+ } else {
+ JS::RootedObject exc_obj(cx, &exc.toObject());
+ JSErrorReport* report = JS_ErrorFromException(cx, exc_obj);
+ couch_error(cx, report);
+ }
return 1;
}
@@ -483,7 +510,14 @@ main(int argc, const char* argv[])
JS::RootedValue result(cx);
if(JS_ExecuteScript(cx, script, &result) != true) {
- fprintf(stderr, "Failed to execute script.\n");
+ JS::RootedValue exc(cx);
+ if(!JS_GetPendingException(cx, &exc)) {
+ fprintf(stderr, "Failed to execute script.\n");
+ } else {
+ JS::RootedObject exc_obj(cx, &exc.toObject());
+ JSErrorReport* report = JS_ErrorFromException(cx, exc_obj);
+ couch_error(cx, report);
+ }
return 1;
}