Commit 8d3e657e authored by Evan Welsh's avatar Evan Welsh

modules: Split print functions into native module.

parent 3e3142c2
Pipeline #185079 failed with stages
in 23 minutes and 11 seconds
......@@ -88,145 +88,6 @@ run_bootstrap(JSContext *cx,
return JS::CloneAndExecuteScript(cx, compiled_script, &ignored);
}
GJS_JSAPI_RETURN_CONVENTION
static bool
gjs_log(JSContext *cx,
unsigned argc,
JS::Value *vp)
{
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
if (argc != 1) {
gjs_throw(cx, "Must pass a single argument to log()");
return false;
}
/* JS::ToString might throw, in which case we will only log that the value
* could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
JS::RootedString jstr(cx, JS::ToString(cx, argv[0]));
exc_state.restore();
if (!jstr) {
g_message("JS LOG: <cannot convert value to string>");
return true;
}
JS::UniqueChars s(JS_EncodeStringToUTF8(cx, jstr));
if (!s)
return false;
g_message("JS LOG: %s", s.get());
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool
gjs_log_error(JSContext *cx,
unsigned argc,
JS::Value *vp)
{
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
if ((argc != 1 && argc != 2) || !argv[0].isObject()) {
gjs_throw(cx, "Must pass an exception and optionally a message to logError()");
return false;
}
JS::RootedString jstr(cx);
if (argc == 2) {
/* JS::ToString might throw, in which case we will only log that the
* value could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
jstr = JS::ToString(cx, argv[1]);
exc_state.restore();
}
gjs_log_exception_full(cx, argv[0], jstr, G_LOG_LEVEL_WARNING);
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool
gjs_print_parse_args(JSContext *cx,
const JS::CallArgs& argv,
GjsAutoChar *buffer)
{
GString *str;
guint n;
str = g_string_new("");
for (n = 0; n < argv.length(); ++n) {
/* JS::ToString might throw, in which case we will only log that the
* value could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
JS::RootedString jstr(cx, JS::ToString(cx, argv[n]));
exc_state.restore();
if (jstr) {
JS::UniqueChars s(JS_EncodeStringToUTF8(cx, jstr));
if (!s) {
g_string_free(str, true);
return false;
}
g_string_append(str, s.get());
if (n < (argv.length()-1))
g_string_append_c(str, ' ');
} else {
*buffer = g_string_free(str, true);
if (!*buffer)
*buffer = g_strdup("<invalid string>");
return true;
}
}
*buffer = g_string_free(str, false);
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool
gjs_print(JSContext *context,
unsigned argc,
JS::Value *vp)
{
JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
GjsAutoChar buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
g_print("%s\n", buffer.get());
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool
gjs_printerr(JSContext *context,
unsigned argc,
JS::Value *vp)
{
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
GjsAutoChar buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
g_printerr("%s\n", buffer.get());
argv.rval().setUndefined();
return true;
}
const JSClassOps defaultclassops = JS::DefaultGlobalClassOps;
class GjsGlobal {
......@@ -237,10 +98,6 @@ class GjsGlobal {
};
static constexpr JSFunctionSpec static_funcs[] = {
JS_FN("log", gjs_log, 1, GJS_MODULE_PROP_FLAGS),
JS_FN("logError", gjs_log_error, 2, GJS_MODULE_PROP_FLAGS),
JS_FN("print", gjs_print, 0, GJS_MODULE_PROP_FLAGS),
JS_FN("printerr", gjs_printerr, 0, GJS_MODULE_PROP_FLAGS),
JS_FS_END};
public:
......
......@@ -387,6 +387,7 @@ libgjs_sources = [
'gjs/stack.cpp',
'modules/console.cpp', 'modules/console.h',
'modules/modules.cpp', 'modules/modules.h',
'modules/print.cpp', 'modules/print.h',
'modules/system.cpp', 'modules/system.h',
]
......
......@@ -26,6 +26,7 @@
#include "gjs/native.h"
#include "modules/console.h"
#include "modules/modules.h"
#include "modules/print.h"
#include "modules/system.h"
#ifdef ENABLE_CAIRO
......@@ -40,4 +41,5 @@ gjs_register_static_modules (void)
#endif
gjs_register_native_module("system", gjs_js_define_system_stuff);
gjs_register_native_module("console", gjs_define_console_stuff);
gjs_register_native_module("print", gjs_define_print_stuff);
}
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <config.h> // for HAVE_READLINE_READLINE_H
#include <stdio.h> // for fputc, fputs, stderr, size_t, fflush
#include <string.h> // for strchr
#ifdef HAVE_READLINE_READLINE_H
# include <readline/history.h>
# include <readline/readline.h>
#endif
#include <glib.h>
#include <glib/gprintf.h> // for g_fprintf
#include "gjs/atoms.h"
#include "gjs/context-private.h"
#include "gjs/jsapi-util.h"
#include "js/CompilationAndEvaluation.h"
#include "js/Conversions.h"
#include "js/SourceText.h"
#include "js/Warnings.h"
#include "modules/print.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_log(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
if (argc != 1) {
gjs_throw(cx, "Must pass a single argument to log()");
return false;
}
/* JS::ToString might throw, in which case we will only log that the value
* could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
JS::RootedString jstr(cx, JS::ToString(cx, argv[0]));
exc_state.restore();
if (!jstr) {
g_message("JS LOG: <cannot convert value to string>");
return true;
}
JS::UniqueChars s(JS_EncodeStringToUTF8(cx, jstr));
if (!s)
return false;
g_message("JS LOG: %s", s.get());
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_log_error(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
if ((argc != 1 && argc != 2) || !argv[0].isObject()) {
gjs_throw(
cx,
"Must pass an exception and optionally a message to logError()");
return false;
}
JS::RootedString jstr(cx);
if (argc == 2) {
/* JS::ToString might throw, in which case we will only log that the
* value could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
jstr = JS::ToString(cx, argv[1]);
exc_state.restore();
}
gjs_log_exception_full(cx, argv[0], jstr, G_LOG_LEVEL_WARNING);
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_print_parse_args(JSContext* cx, const JS::CallArgs& argv,
GjsAutoChar* buffer) {
GString* str;
guint n;
str = g_string_new("");
for (n = 0; n < argv.length(); ++n) {
/* JS::ToString might throw, in which case we will only log that the
* value could not be converted to string */
JS::AutoSaveExceptionState exc_state(cx);
JS::RootedString jstr(cx, JS::ToString(cx, argv[n]));
exc_state.restore();
if (jstr) {
JS::UniqueChars s(JS_EncodeStringToUTF8(cx, jstr));
if (!s) {
g_string_free(str, true);
return false;
}
g_string_append(str, s.get());
if (n < (argv.length() - 1))
g_string_append_c(str, ' ');
} else {
*buffer = g_string_free(str, true);
if (!*buffer)
*buffer = g_strdup("<invalid string>");
return true;
}
}
*buffer = g_string_free(str, false);
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_print(JSContext* context, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
GjsAutoChar buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
g_print("%s\n", buffer.get());
argv.rval().setUndefined();
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_printerr(JSContext* context, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
GjsAutoChar buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
g_printerr("%s\n", buffer.get());
argv.rval().setUndefined();
return true;
}
static constexpr JSFunctionSpec funcs[] = {
JS_FN("log", gjs_log, 1, GJS_MODULE_PROP_FLAGS),
JS_FN("logError", gjs_log_error, 2, GJS_MODULE_PROP_FLAGS),
JS_FN("print", gjs_print, 0, GJS_MODULE_PROP_FLAGS),
JS_FN("printerr", gjs_printerr, 0, GJS_MODULE_PROP_FLAGS), JS_FS_END};
bool gjs_define_print_stuff(JSContext* context,
JS::MutableHandleObject module) {
module.set(JS_NewPlainObject(context));
return JS_DefineFunctions(context, module, funcs);
}
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Copyright (c) 2008 litl, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef MODULES_PRINT_H_
#define MODULES_PRINT_H_
#include <config.h>
#include <js/TypeDecls.h>
#include "gjs/macros.h"
GJS_JSAPI_RETURN_CONVENTION
bool gjs_define_print_stuff(JSContext* context, JS::MutableHandleObject module);
#endif // MODULES_PRINT_H_
---
extends: "../../../.eslintrc.yml"
globals:
log: "off"
logError: "off"
print: "off"
printerr: "off"
......@@ -16,6 +16,8 @@
* the first frame is executed.
*/
const {print, logError} = imports.print;
// Debugger state.
var focusedFrame = null;
var topFrame = null;
......
(function (exports) {
'use strict';
// Do early initialization here.
void exports;
const {print, printerr, log, logError} = imports.print;
Object.defineProperties(exports, {
print: {
configurable: true,
enumerable: true,
value: print,
},
printerr: {
configurable: true,
enumerable: true,
value: printerr,
},
log: {
configurable: true,
enumerable: true,
value: log,
},
logError: {
configurable: true,
enumerable: true,
value: logError,
},
});
})(globalThis);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment