Skip to content

Commit

Permalink
Cleanup of graal-nodejs unit tests.
Browse files Browse the repository at this point in the history
PullRequest: js/675
  • Loading branch information
iamstolis committed Dec 19, 2018
2 parents 4653a6a + f7520c4 commit 3e0ebb9
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 23 deletions.
1 change: 1 addition & 0 deletions graal-nodejs/deps/v8/src/graal/graal_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ GraalIsolate::GraalIsolate(JavaVM* jvm, JNIEnv* env) : function_template_data(),
ACCESS_METHOD(GraalAccessMethod::stack_frame_get_column, "stackFrameGetColumn", "(Ljava/lang/Object;)I")
ACCESS_METHOD(GraalAccessMethod::stack_frame_get_script_name, "stackFrameGetScriptName", "(Ljava/lang/Object;)Ljava/lang/Object;")
ACCESS_METHOD(GraalAccessMethod::stack_frame_get_function_name, "stackFrameGetFunctionName", "(Ljava/lang/Object;)Ljava/lang/Object;")
ACCESS_METHOD(GraalAccessMethod::stack_frame_is_eval, "stackFrameIsEval", "(Ljava/lang/Object;)Z")
ACCESS_METHOD(GraalAccessMethod::make_weak, "makeWeak", "(Ljava/lang/Object;JJJI)V")
ACCESS_METHOD(GraalAccessMethod::clear_weak, "clearWeak", "(Ljava/lang/Object;J)J")
ACCESS_METHOD(GraalAccessMethod::string_external_resource_callback, "stringExternalResourceCallback", "(Ljava/lang/Object;JJ)V")
Expand Down
1 change: 1 addition & 0 deletions graal-nodejs/deps/v8/src/graal/graal_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ enum GraalAccessMethod {
stack_frame_get_column,
stack_frame_get_script_name,
stack_frame_get_function_name,
stack_frame_is_eval,
make_weak,
clear_weak,
string_external_resource_callback,
Expand Down
5 changes: 5 additions & 0 deletions graal-nodejs/deps/v8/src/graal/graal_stack_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ v8::Local<v8::String> GraalStackFrame::GetFunctionName() const {
GraalString* graal_function_name = new GraalString(Isolate(), (jstring) function_name);
return reinterpret_cast<v8::String*> (graal_function_name);
}

bool GraalStackFrame::IsEval() const {
JNI_CALL(jboolean, result, Isolate(), GraalAccessMethod::stack_frame_is_eval, Boolean, GetJavaObject());
return result;
}
1 change: 1 addition & 0 deletions graal-nodejs/deps/v8/src/graal/graal_stack_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class GraalStackFrame : public GraalHandleContent {
int GetColumn() const;
v8::Local<v8::String> GetScriptName() const;
v8::Local<v8::String> GetFunctionName() const;
bool IsEval() const;
protected:
GraalHandleContent* CopyImpl(jobject java_object_copy) override;
};
Expand Down
16 changes: 16 additions & 0 deletions graal-nodejs/deps/v8/src/graal/graal_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,19 @@ v8::Local<v8::String> GraalString::NewExternal(v8::Isolate* isolate, v8::String:
}
return result;
}

bool GraalString::ContainsOnlyOneByte() const {
jstring java_string = (jstring) GetJavaObject();
JNIEnv* env = Isolate()->GetJNIEnv();
int length = env->GetStringLength(java_string);
const jchar* data = env->GetStringCritical(java_string, nullptr);
bool onlyOneByte = true;
for (int i = 0; i < length; i++) {
if (data[i] >= 256) {
onlyOneByte = false;
break;
}
}
env->ReleaseStringCritical(java_string, data);
return onlyOneByte;
}
1 change: 1 addition & 0 deletions graal-nodejs/deps/v8/src/graal/graal_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GraalString : public GraalName {
int WriteUtf8(char* buffer, int length, int* nchars_ref, int options) const;
int WriteOneByte(uint8_t* buffer, int start, int length, int options) const;
int Write(uint16_t* buffer, int start, int length, int options) const;
bool ContainsOnlyOneByte() const;

/* Determines whether the given character is a continuation byte in UTF8 */
static inline bool IsContinuationByte(const unsigned char c) {
Expand Down
10 changes: 6 additions & 4 deletions graal-nodejs/deps/v8/src/graal/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1404,8 +1404,7 @@ namespace v8 {
}

bool StackFrame::IsEval() const {
TRACE
return false;
return reinterpret_cast<const GraalStackFrame*> (this)->IsEval();
}

int StackTrace::GetFrameCount() const {
Expand All @@ -1427,8 +1426,11 @@ namespace v8 {
}

bool String::IsOneByte() const {
TRACE
return false;
return reinterpret_cast<const GraalString*> (this)->ContainsOnlyOneByte();
}

bool String::ContainsOnlyOneByte() const {
return reinterpret_cast<const GraalString*> (this)->ContainsOnlyOneByte();
}

int String::Length() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,11 @@ public Object stackFrameGetFunctionName(Object stackFrame) {
return element.getFunctionName();
}

public boolean stackFrameIsEval(Object stackFrame) {
GraalJSException.JSStackTraceElement element = (GraalJSException.JSStackTraceElement) stackFrame;
return element.isEval();
}

/**
* Key for a weak callback.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
{ "name": "stackFrameGetFunctionName" },
{ "name": "stackFrameGetLineNumber" },
{ "name": "stackFrameGetScriptName" },
{ "name": "stackFrameIsEval" },
{ "name": "stackTraceCurrentStackTrace" },
{ "name": "stringExternalResourceCallback" },
{ "name": "stringObjectNew" },
Expand Down
8 changes: 4 additions & 4 deletions graal-nodejs/test/graal/unit/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
var assert = require('assert');
var vm = require('vm');

describe('Interop', function () {
if (typeof Interop === 'object') {
describe('Polyglot', function () {
if (typeof Polyglot === 'object') {
it('eval should work in a new context', function () {
assert.strictEqual(require('vm').runInNewContext("Interop.eval('application/javascript', '7*6')"), 42);
assert.strictEqual(require('vm').runInNewContext("Polyglot.eval('application/javascript', '7*6')"), 42);
});
it('export/import should work in a new context', function () {
assert.strictEqual(require('vm').runInNewContext("Interop.export('obj', { foo: 'bar' }); Interop.import('obj').foo"), 'bar');
assert.strictEqual(require('vm').runInNewContext("Polyglot.export('obj', { foo: 'bar' }); Polyglot.import('obj').foo"), 'bar');
});
}
});
3 changes: 1 addition & 2 deletions graal-nodejs/test/graal/unit/stacktrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ EXPORT_TO_JS(FrameGetScriptName) {
EXPORT_TO_JS(FrameGetScriptId) {
Local<StackTrace> trace = createStackTrace(args);
Local<StackFrame> frame = trace->GetFrame(0);
//int id = frame->GetScriptId(); //TODO not available currently
int id = -frame->GetLineNumber(); //negative number, ensures we fail
int id = frame->GetScriptId();
args.GetReturnValue().Set(id);
}

Expand Down
5 changes: 2 additions & 3 deletions graal-nodejs/test/graal/unit/stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ describe('StackFrame', function () {
var isEval = module.StackTrace_FrameIsEval(thrower);
assert.strictEqual(isEval, false);
});
it.skip('should be inside eval', function () { //never returns true for any frame, on Node.js
it('should be inside eval', function () { //never returns true for any frame, on Node.js
var isEval = eval("function test() { return module.StackTrace_FrameIsEval(); }; test();");
assert.strictEqual(isEval, true);
});
});
describe.skip('GetScriptId', function () { //not supported by our v8.h
describe.skipOnGraal('GetScriptId', function () {
it('should return an integer', function () {
var id = module.StackTrace_FrameGetScriptId();
console.log(id);
assert.strictEqual(typeof id, "number");
assert.strictEqual(id > 0, true);
});
Expand Down
2 changes: 1 addition & 1 deletion graal-nodejs/test/graal/unit/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('String', function () {
assert.strictEqual(module.String_IsExternalOneByte(""), false);
});
});
describe.skip('IsOneByte', function () {
describe('IsOneByte', function () {
it('should return true for ""', function () {
assert.strictEqual(module.String_IsOneByte(""), true);
});
Expand Down
5 changes: 2 additions & 3 deletions graal-nodejs/test/graal/unit/try_catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ describe('TryCatch', function () {
assert.strictEqual(typeof line, "string");
});
});
describe.skip('HasTerminated', function () {
describe('HasTerminated', function () {
it('should be callable', function () {
module.TryCatch_HasTerminatedNoException();
assert.strictEqual(true, true);
assert.strictEqual(module.TryCatch_HasTerminatedNoException(), false);
});
});
describe('Fatal Error', function () {
Expand Down
10 changes: 7 additions & 3 deletions graal-nodejs/test/graal/unit/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ EXPORT_TO_JS(ToBoolean) {
EXPORT_TO_JS(ToNumber) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
args.GetReturnValue().Set(args[0]->ToNumber(context).ToLocalChecked());
MaybeLocal<Number> number = args[0]->ToNumber(context);
if (number.IsEmpty()) {
return;
}
args.GetReturnValue().Set(number.ToLocalChecked());
}

EXPORT_TO_JS(ToString) {
Expand All @@ -376,13 +380,13 @@ EXPORT_TO_JS(ToInteger) {
EXPORT_TO_JS(ToUint32) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
args.GetReturnValue().Set(args[0]->ToUint32(context).ToLocalChecked());
args.GetReturnValue().Set(args[0]->ToUint32(context).ToLocalChecked()->Value());
}

EXPORT_TO_JS(ToInt32) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
args.GetReturnValue().Set(args[0]->ToInt32(context).ToLocalChecked());
args.GetReturnValue().Set(args[0]->ToInt32(context).ToLocalChecked()->Value());
}

EXPORT_TO_JS(ToArrayIndex) {
Expand Down
6 changes: 3 additions & 3 deletions graal-nodejs/test/graal/unit/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ describe('Value - To*()', function () {
assert.strictEqual(module.Value_ToNumber("3.1415"), 3.1415);
assert.strictEqual(module.Value_ToNumber("-123456789"), -123456789);
});
it.skip('should throw for symbol input', function () { //requires ECMAScript 2015
it('should throw for symbol input', function () {
assert.throws(function () {
module.Value_ToNumber(Symbol("test"));
}, TypeError);
Expand Down Expand Up @@ -858,9 +858,9 @@ describe('Value - To*()', function () {
assert.strictEqual(module.Value_ToUint32(two32), 0);
assert.strictEqual(module.Value_ToUint32(two32 + 1), 1);
});
it.skip('should convert outside range (failing on Node 6.2.2)', function () {
it('should convert outside range', function () {
var two32 = Math.pow(2, 32);
assert.strictEqual(module.Value_ToUint32(-1), two32 - 1); //node 6.2.2 off spec?
assert.strictEqual(module.Value_ToUint32(-1), two32 - 1);
assert.strictEqual(module.Value_ToUint32(-2), two32 - 2);
});
});
Expand Down

0 comments on commit 3e0ebb9

Please sign in to comment.