summaryrefslogtreecommitdiff
path: root/test/addons/non-node-context
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2018-10-28 15:59:20 +0100
committerRich Trott <rtrott@gmail.com>2018-11-03 23:31:03 -0700
commit9c7d19104d1f03f70658a6834a7d0baac7d38724 (patch)
tree697662a674e093029e0b0681a80a2548dbee1d6e /test/addons/non-node-context
parentd8fb81fab3042b0229e11e82f88ed062b171036a (diff)
downloadandroid-node-v8-9c7d19104d1f03f70658a6834a7d0baac7d38724.tar.gz
android-node-v8-9c7d19104d1f03f70658a6834a7d0baac7d38724.tar.bz2
android-node-v8-9c7d19104d1f03f70658a6834a7d0baac7d38724.zip
buffer: throw exception when creating from non-Node.js Context
Throw an exception instead of crashing when attempting to create `Buffer` objects from a Context that is not associated with a Node.js `Environment`. Possible alternatives for the future might be just returning a plain `Uint8Array`, or working on providing `Buffer` for all `Context`s. PR-URL: https://github.com/nodejs/node/pull/23938 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/addons/non-node-context')
-rw-r--r--test/addons/non-node-context/binding.cc21
-rw-r--r--test/addons/non-node-context/test-make-buffer.js22
2 files changed, 36 insertions, 7 deletions
diff --git a/test/addons/non-node-context/binding.cc b/test/addons/non-node-context/binding.cc
index 324f5c5a1e..776786cef5 100644
--- a/test/addons/non-node-context/binding.cc
+++ b/test/addons/non-node-context/binding.cc
@@ -1,4 +1,5 @@
#include <node.h>
+#include <node_buffer.h>
#include <assert.h>
namespace {
@@ -15,6 +16,17 @@ using v8::Script;
using v8::String;
using v8::Value;
+inline void MakeBufferInNewContext(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ Isolate* isolate = args.GetIsolate();
+ Local<Context> context = Context::New(isolate);
+ Context::Scope context_scope(context);
+
+ // This should throw an exception, rather than actually do anything.
+ MaybeLocal<Object> buf = node::Buffer::Copy(isolate, "foo", 3);
+ assert(buf.IsEmpty());
+}
+
inline void RunInNewContext(
const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
@@ -41,13 +53,8 @@ inline void RunInNewContext(
inline void Initialize(Local<Object> exports,
Local<Value> module,
Local<Context> context) {
- Isolate* isolate = context->GetIsolate();
- Local<String> key = String::NewFromUtf8(
- isolate, "runInNewContext", NewStringType::kNormal).ToLocalChecked();
- Local<Function> value = FunctionTemplate::New(isolate, RunInNewContext)
- ->GetFunction(context)
- .ToLocalChecked();
- assert(exports->Set(context, key, value).IsJust());
+ NODE_SET_METHOD(exports, "runInNewContext", RunInNewContext);
+ NODE_SET_METHOD(exports, "makeBufferInNewContext", MakeBufferInNewContext);
}
} // anonymous namespace
diff --git a/test/addons/non-node-context/test-make-buffer.js b/test/addons/non-node-context/test-make-buffer.js
new file mode 100644
index 0000000000..9b17fa4ee9
--- /dev/null
+++ b/test/addons/non-node-context/test-make-buffer.js
@@ -0,0 +1,22 @@
+'use strict';
+
+const common = require('../../common');
+const assert = require('assert');
+const {
+ makeBufferInNewContext
+} = require(`./build/${common.buildType}/binding`);
+
+// Because the `Buffer` function and its protoype property only (currently)
+// exist in a Node.js instance’s main context, trying to create buffers from
+// another context throws an exception.
+
+try {
+ makeBufferInNewContext();
+} catch (exception) {
+ assert.strictEqual(exception.constructor.name, 'Error');
+ assert(!(exception.constructor instanceof Error));
+
+ assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
+ assert.strictEqual(exception.message,
+ 'Buffer is not available for the current Context');
+}