#ifndef V8SCRIPTUTILS_H #define V8SCRIPTUTILS_H #include // Throw an exception if the function was called with new Function(); #define V8ENV_THROW_CONSTRUCTOR(args, isolate) \ if (args.IsConstructCall()) \ { \ isolate->ThrowException(v8::String::NewFromUtf8Literal(isolate, \ "Cannot call function as a constructor.")); \ return; \ } // Throw an exception if a constructor was called with Function(); #define V8ENV_THROW_METHOD(args, isolate) \ if (!args.IsConstructCall()) \ { \ isolate->ThrowException(v8::String::NewFromUtf8Literal(isolate, \ "Cannot call constructor as a function.")); \ return; \ } // Throw an exception if we didn't receive the correct amount of arguments #define V8ENV_THROW_ARGCOUNT(args, isolate, required_args) \ if (args.Length() != required_args) \ { \ isolate->ThrowException(v8::String::NewFromUtf8(isolate, \ std::string("Cannot call function with ") \ .append(std::to_string(args.Length())) \ .append(" arguments, required " #required_args) \ .c_str()) \ .ToLocalChecked()); \ return; \ } // Throw an exception if we receive less than the minimum amount of arguments #define V8ENV_THROW_MINARGCOUNT(args, isolate, required_args) \ if (args.Length() < required_args) \ { \ isolate->ThrowException(v8::String::NewFromUtf8(isolate, \ std::string("Cannot call function with ") \ .append(std::to_string(args.Length())) \ .append(" arguments, required " #required_args) \ .c_str()) \ .ToLocalChecked()); \ return; \ } // Unwrap an object, and validate the pointer #define V8ENV_SAFE_UNWRAP(ARGS, TYPE, VAR_NAME) \ TYPE* VAR_NAME = unwrapObject(ARGS.This()); \ if (!VAR_NAME) \ { \ return; \ } template inline v8::Local persistentToLocal(v8::Isolate* isolate, const v8::Persistent& persistent) { if (persistent.IsWeak()) { return v8::Local::New(isolate, persistent); } else { return *reinterpret_cast*>(const_cast*>(&persistent)); } } template inline v8::Local globalPersistentToLocal(v8::Isolate* isolate, const v8::Global& persistent) { if (persistent.IsWeak()) { return v8::Local::New(isolate, persistent); } else { return *reinterpret_cast*>(const_cast*>(&persistent)); } } template inline Type* unwrapObject(v8::Local self) { return static_cast(self->GetAlignedPointerFromInternalField(0)); } #endif