/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2018 NAN contributors * * MIT License ********************************************************************/ #include #if defined(_MSC_VER) # pragma warning( push ) # pragma warning( disable : 4530 ) # include # pragma warning( pop ) #else # include #endif using namespace Nan; // NOLINT(build/namespaces) static int magic = 1337; NAN_METHOD(NewNumber) { info.GetReturnValue().Set(New(0.5)); } NAN_METHOD(NewNegativeInteger) { info.GetReturnValue().Set(New(-1)); } NAN_METHOD(NewPositiveInteger) { info.GetReturnValue().Set(New(1)); } NAN_METHOD(NewUnsignedInteger) { info.GetReturnValue().Set(New(0xFFFFFFFFu)); } NAN_METHOD(NewInt32FromPositive) { info.GetReturnValue().Set(New(0xFFFFFFFF)); } NAN_METHOD(NewInt32FromNegative) { info.GetReturnValue().Set(New(-1)); } NAN_METHOD(NewUint32FromPositive) { info.GetReturnValue().Set(New(0xFFFFFFFF)); } NAN_METHOD(NewUint32FromNegative) { info.GetReturnValue().Set(New(-1)); } NAN_METHOD(NewUtf8String) { const char s[] = "strïng"; info.GetReturnValue().Set(New(s).ToLocalChecked()); } NAN_METHOD(NewLatin1String) { const uint8_t s[] = "str\xefng"; info.GetReturnValue().Set(NewOneByteString(s).ToLocalChecked()); } NAN_METHOD(NewUcs2String) { const uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; info.GetReturnValue().Set(New(s).ToLocalChecked()); } NAN_METHOD(NewStdString) { const std::string s = "strïng"; info.GetReturnValue().Set(New(s).ToLocalChecked()); } NAN_METHOD(NewRegExp) { info.GetReturnValue().Set( New(New("foo").ToLocalChecked() , v8::RegExp::kNone).ToLocalChecked()); } NAN_METHOD(NewStringObject) { info.GetReturnValue().Set( New(New("foo").ToLocalChecked())); } NAN_METHOD(NewNumberObject) { info.GetReturnValue().Set(New(0.5)); } NAN_METHOD(NewBooleanObject) { info.GetReturnValue().Set(New(true)); } NAN_METHOD(NewExternal) { v8::Local ext = New(&magic); assert(*static_cast(ext->Value()) == 1337); info.GetReturnValue().Set(New("passed").ToLocalChecked()); } NAN_METHOD(NewSignature) { v8::Local tmpl = New(NewSignature); v8::Local sig = New(tmpl); tmpl = New( NewSignature, v8::Local(), sig); info.GetReturnValue().Set(New("string").ToLocalChecked()); } NAN_METHOD(NewScript) { v8::Local script = New(New("2+4").ToLocalChecked()).ToLocalChecked(); info.GetReturnValue().Set( To(RunScript(script).ToLocalChecked()).ToLocalChecked()); } NAN_METHOD(NewScript2) { ScriptOrigin origin( New("x").ToLocalChecked()); v8::Local script = New( New("2+4").ToLocalChecked() , origin).ToLocalChecked(); info.GetReturnValue().Set( To(RunScript(script).ToLocalChecked()).ToLocalChecked()); } NAN_METHOD(CompileScript) { v8::Local script = CompileScript(New("2+4").ToLocalChecked()).ToLocalChecked(); info.GetReturnValue().Set( To(RunScript(script).ToLocalChecked()).ToLocalChecked()); } NAN_METHOD(CompileScript2) { ScriptOrigin origin( New("x").ToLocalChecked()); v8::Local script = CompileScript(New("2+4").ToLocalChecked(), origin).ToLocalChecked(); info.GetReturnValue().Set( To(RunScript(script).ToLocalChecked()).ToLocalChecked()); } NAN_METHOD(NewDate) { info.GetReturnValue().Set(New(1337).ToLocalChecked()); } NAN_METHOD(NewArray) { info.GetReturnValue().Set(New()); } NAN_METHOD(NewBoolean) { info.GetReturnValue().Set(New(true)); } // #212 NAN_METHOD(NewBoolean2) { #if defined(_MSC_VER) # pragma warning( push ) # pragma warning( disable : 4800 ) #endif info.GetReturnValue().Set(New(1)); #if defined(_MSC_VER) # pragma warning( pop ) #endif } NAN_MODULE_INIT(Init) { Set(target , New("newNumber").ToLocalChecked() , GetFunction(New(NewNumber)).ToLocalChecked() ); Set(target , New("newNegativeInteger").ToLocalChecked() , GetFunction(New(NewNegativeInteger)) .ToLocalChecked() ); Set(target , New("newPositiveInteger").ToLocalChecked() , GetFunction(New(NewPositiveInteger)) .ToLocalChecked() ); Set(target , New("newUnsignedInteger").ToLocalChecked() , GetFunction(New(NewUnsignedInteger)) .ToLocalChecked() ); Set(target , New("newInt32FromPositive").ToLocalChecked() , GetFunction(New(NewInt32FromPositive)) .ToLocalChecked() ); Set(target , New("newInt32FromNegative").ToLocalChecked() , GetFunction(New(NewInt32FromNegative)) .ToLocalChecked() ); Set(target , New("newUint32FromPositive").ToLocalChecked() , GetFunction(New(NewUint32FromPositive)) .ToLocalChecked() ); Set(target , New("newUint32FromNegative").ToLocalChecked() , GetFunction(New(NewUint32FromNegative)) .ToLocalChecked() ); Set(target , New("newUtf8String").ToLocalChecked() , GetFunction(New(NewUtf8String)).ToLocalChecked() ); Set(target , New("newLatin1String").ToLocalChecked() , GetFunction(New(NewLatin1String)).ToLocalChecked() ); Set(target , New("newUcs2String").ToLocalChecked() , GetFunction(New(NewUcs2String)).ToLocalChecked() ); Set(target , New("newStdString").ToLocalChecked() , GetFunction(New(NewStdString)).ToLocalChecked() ); Set(target , New("newRegExp").ToLocalChecked() , GetFunction(New(NewRegExp)).ToLocalChecked() ); Set(target , New("newStringObject").ToLocalChecked() , GetFunction(New(NewStringObject)).ToLocalChecked() ); Set(target , New("newNumberObject").ToLocalChecked() , GetFunction(New(NewNumberObject)).ToLocalChecked() ); Set(target , New("newBooleanObject").ToLocalChecked() , GetFunction(New(NewBooleanObject)).ToLocalChecked() ); Set(target , New("newExternal").ToLocalChecked() , GetFunction(New(NewExternal)).ToLocalChecked() ); Set(target , New("newSignature").ToLocalChecked() , GetFunction(New(NewSignature)).ToLocalChecked() ); Set(target , New("newScript").ToLocalChecked() , GetFunction(New(NewScript)).ToLocalChecked() ); Set(target , New("newScript2").ToLocalChecked() , GetFunction(New(NewScript2)).ToLocalChecked() ); Set(target , New("compileScript").ToLocalChecked() , GetFunction(New(CompileScript)).ToLocalChecked() ); Set(target , New("compileScript2").ToLocalChecked() , GetFunction(New(CompileScript2)).ToLocalChecked() ); Set(target , New("newDate").ToLocalChecked() , GetFunction(New(NewDate)).ToLocalChecked() ); Set(target , New("newArray").ToLocalChecked() , GetFunction(New(NewArray)).ToLocalChecked() ); Set(target , New("newBoolean").ToLocalChecked() , GetFunction(New(NewBoolean)).ToLocalChecked() ); Set(target , New("newBoolean2").ToLocalChecked() , GetFunction(New(NewBoolean2)).ToLocalChecked() ); } NODE_MODULE(news, Init)