Commit 83e7d7c7 authored by Philip Chimento's avatar Philip Chimento 🚮

Merge branch 'compatibility-bytearray-tostring' into 'master'

byteArray: Add compatibility toString property

See merge request GNOME/gjs!227
parents 1043d492 8f0dff8d
......@@ -59,6 +59,8 @@ gjs_srcs = \
gjs/context-private.h \
gjs/coverage.cpp \
gjs/debugger.cpp \
gjs/deprecation.cpp \
gjs/deprecation.h \
gjs/engine.cpp \
gjs/engine.h \
gjs/global.cpp \
......
......@@ -25,6 +25,7 @@
#include "byteArray.h"
#include "gi/boxed.h"
#include "gjs/deprecation.h"
#include "jsapi-util-args.h"
#include "jsapi-wrapper.h"
......@@ -45,22 +46,11 @@ static void bytes_unref_arraybuffer(void* contents, void* user_data) {
}
/* implement toString() with an optional encoding arg */
static bool
to_string_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
GjsAutoJSChar encoding;
JS::RootedObject byte_array(context);
static bool to_string_impl(JSContext* context, JS::HandleObject byte_array,
const char* encoding, JS::MutableHandleValue rval) {
bool encoding_is_utf8;
uint8_t* data;
if (!gjs_parse_call_args(context, "toString", argv, "o|s",
"byteArray", &byte_array,
"encoding", &encoding))
return false;
if (encoding) {
/* maybe we should be smarter about utf8 synonyms here.
* doesn't matter much though. encoding_is_utf8 is
......@@ -80,7 +70,7 @@ to_string_func(JSContext *context,
* libmozjs hardwired utf8-to-utf16
*/
return gjs_string_from_utf8_n(context, reinterpret_cast<char*>(data),
len, argv.rval());
len, rval);
} else {
bool ok = false;
gsize bytes_written;
......@@ -109,7 +99,7 @@ to_string_func(JSContext *context,
s = JS_NewUCStringCopyN(context, u16_out, bytes_written / 2);
if (s != NULL) {
ok = true;
argv.rval().setString(s);
rval.setString(s);
}
g_free(u16_str);
......@@ -118,6 +108,35 @@ to_string_func(JSContext *context,
}
}
static bool to_string_func(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
GjsAutoJSChar encoding;
JS::RootedObject byte_array(cx);
if (!gjs_parse_call_args(cx, "toString", args, "o|s", "byteArray",
&byte_array, "encoding", &encoding))
return false;
return to_string_impl(cx, byte_array, encoding, args.rval());
}
/* Workaround to keep existing code compatible. This function is tacked onto
* any Uint8Array instances created in situations where previously a ByteArray
* would have been created. It logs a compatibility warning. */
static bool instance_to_string_func(JSContext* cx, unsigned argc,
JS::Value* vp) {
GJS_GET_THIS(cx, argc, vp, args, this_obj);
GjsAutoJSChar encoding;
_gjs_warn_deprecated_once_per_callsite(
cx, GjsDeprecationMessageId::ByteArrayInstanceToString);
if (!gjs_parse_call_args(cx, "toString", args, "|s", "encoding", &encoding))
return false;
return to_string_impl(cx, this_obj, encoding, args.rval());
}
static bool
to_gbytes_func(JSContext *context,
unsigned argc,
......@@ -227,6 +246,7 @@ from_string_func(JSContext *context,
if (!array_buffer)
return false;
obj = JS_NewUint8ArrayWithBuffer(context, array_buffer, 0, -1);
JS_DefineFunction(context, obj, "toString", instance_to_string_func, 1, 0);
argv.rval().setObject(*obj);
return true;
}
......@@ -264,6 +284,7 @@ from_gbytes_func(JSContext *context,
context, JS_NewUint8ArrayWithBuffer(context, array_buffer, 0, -1));
if (!obj)
return false;
JS_DefineFunction(context, obj, "toString", instance_to_string_func, 1, 0);
argv.rval().setObject(*obj);
return true;
......@@ -275,7 +296,10 @@ JSObject* gjs_byte_array_from_data(JSContext* cx, size_t nbytes, void* data) {
if (!array_buffer)
return nullptr;
return JS_NewUint8ArrayWithBuffer(cx, array_buffer, 0, -1);
JS::RootedObject array(cx,
JS_NewUint8ArrayWithBuffer(cx, array_buffer, 0, -1));
JS_DefineFunction(cx, array, "toString", instance_to_string_func, 1, 0);
return array;
}
JSObject* gjs_byte_array_from_byte_array(JSContext* cx, GByteArray* array) {
......
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Copyright (c) 2018 Philip Chimento <philip.chimento@gmail.com>
*
* 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.
*/
#include <string>
#include <unordered_set>
#include "gjs/context-private.h"
#include "gjs/deprecation.h"
#include "gjs/jsapi-util.h"
#include "gjs/jsapi-wrapper.h"
const char* messages[] = {
// None:
"(invalid message)",
// ByteArrayInstanceToString:
"Some code called array.toString() on a Uint8Array instance. Previously "
"this would have interpreted the bytes of the array as a string, but that "
"is nonstandard. In the future this will return the bytes as "
"comma-separated digits. For the time being, the old behavior has been "
"preserved, but please fix your code anyway to explicitly call ByteArray"
".toString(array).\n"
"(Note that array.toString() may have been called implicitly.)",
};
struct DeprecationEntry {
GjsDeprecationMessageId id;
std::string loc;
DeprecationEntry(GjsDeprecationMessageId an_id, const char* a_loc)
: id(an_id), loc(a_loc) {}
bool operator==(const DeprecationEntry& other) const {
return id == other.id && loc == other.loc;
}
};
namespace std {
template <>
struct hash<DeprecationEntry> {
size_t operator()(const DeprecationEntry& key) const {
return hash<int>()(key.id) ^ hash<std::string>()(key.loc);
}
};
}; // namespace std
static std::unordered_set<DeprecationEntry> logged_messages;
static char* get_callsite(JSContext* cx) {
JS::RootedObject stack_frame(cx);
if (!JS::CaptureCurrentStack(cx, &stack_frame,
JS::StackCapture(JS::MaxFrames(1))) ||
!stack_frame)
return nullptr;
JS::RootedValue v_frame(cx, JS::ObjectValue(*stack_frame));
JS::RootedString frame_string(cx, JS::ToString(cx, v_frame));
if (!frame_string)
return nullptr;
GjsAutoJSChar frame_utf8;
if (!gjs_string_to_utf8(cx, JS::StringValue(frame_string), &frame_utf8))
return nullptr;
return frame_utf8.release();
}
/* Note, this can only be called from the JS thread because it uses the full
* stack dump API and not the "safe" gjs_dumpstack() which can only print to
* stdout or stderr. Do not use this function during GC, for example. */
void _gjs_warn_deprecated_once_per_callsite(JSContext* cx,
const GjsDeprecationMessageId id) {
GjsAutoJSChar callsite = get_callsite(cx);
DeprecationEntry entry(id, callsite);
if (!logged_messages.count(entry)) {
JS::UniqueChars stack_dump = JS::FormatStackDump(cx, nullptr, false,
false, false);
g_warning("%s\n%s", messages[id], stack_dump.get());
logged_messages.insert(std::move(entry));
}
}
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Copyright (c) 2018 Philip Chimento <philip.chimento@gmail.com>
*
* 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 GJS_DEPRECATION_H_
#define GJS_DEPRECATION_H_
#include "gjs/jsapi-wrapper.h"
enum GjsDeprecationMessageId {
None,
ByteArrayInstanceToString,
};
void _gjs_warn_deprecated_once_per_callsite(JSContext* cx,
GjsDeprecationMessageId message);
#endif /* GJS_DEPRECATION_H_ */
const ByteArray = imports.byteArray;
const {GIMarshallingTests, GLib} = imports.gi;
describe('Byte array', function () {
it('can be created from a string', function () {
......@@ -35,4 +36,27 @@ describe('Byte array', function () {
expect(s.length).toEqual(4);
expect(s).toEqual('abcd');
});
describe('legacy toString() behavior', function () {
beforeEach(function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
'Some code called array.toString()*');
});
it('is preserved when created from a string', function () {
let a = ByteArray.fromString('⅜');
expect(a.toString()).toEqual('⅜');
});
it('is preserved when marshalled from GI', function () {
let a = GIMarshallingTests.bytearray_full_return();
expect(() => a.toString()).toThrowError(TypeError,
/malformed UTF-8 character sequence/);
});
afterEach(function () {
GLib.test_assert_expected_messages_internal('Gjs',
'testByteArray.js', 0, 'testToStringCompatibility');
});
});
});
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