Created
February 12, 2014 23:22
-
-
Save FlyingJester/8966643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////////////////////////////// | |
/* | |
TurboSphere Plugin SDK | |
Copyright (c) 2012-2014 Martin McDonough. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
3. The name of the author may not be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
Permission is granted to anyone to use this software for any purpose, including | |
commercial applications, and to alter it and redistribute it freely, subject | |
to the following restrictions: | |
1. The origin of this software must not be misrepresented; you must not | |
claim that you wrote the original software. If you use this software in a | |
product, an acknowledgment in the product documentation would be | |
appreciated but is not required. | |
2. Altered source versions must be plainly marked as such, and must not be | |
misrepresented as being the original software. This notice may not be | |
removed or altered from any source distribution. | |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | |
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY | |
OF SUCH DAMAGE. | |
///// | |
// What is in this file: | |
- Type definitions for code that interacts with JS | |
- Data validation routines for C++ representations of JS data | |
- Forward declatarations of the required plugin functions. This is disabled | |
when PLUGINNAME is not defined, which signifies that this file is being | |
used by the TurboSphere engine binary itself. | |
*/ | |
/////////////////////////////////////////////////////////////////////////////// | |
#ifdef COMMON_PLUGIN_HEAD | |
#error You have included both the new and old plugin headers. This is not a good idea. | |
#endif | |
#ifndef COMMON_PLUGIN_NEW_HEAD | |
#define COMMON_PLUGIN_NEW_HEAD | |
#ifndef PLUGINNAME //In the engine | |
#ifdef _MSC_VER | |
#pragma warning No Plugin Name Specified. This is fine for the engine itself, bad for plugins. | |
#else | |
#warning No Plugin Name Specified. This is fine for the engine itself, bad for plugins. | |
#endif | |
#define PLUGINNAME Engine | |
#define NOPLUGNAME 1 | |
#endif | |
#include "../../v8.h" | |
#include <stacktrace.h> | |
#include <string> | |
#include <cstdarg> | |
#include <cassert> | |
/////////////////////////////////////////////////////////////////////////////// | |
// New and shiny stuff | |
namespace Turbo{ | |
///// | |
// Basic TypeDefs | |
typedef const v8::FunctionCallbackInfo<v8::Value> & JSArguments; | |
typedef void JSFunction; | |
typedef JSFunction* JSFunctionArray; | |
typedef const char* JSFunctionName; | |
typedef const char* JSVariableName; | |
typedef const char** JSNameArray; | |
typedef const char* InitFunction; | |
///// | |
// Internal Typdefs | |
typedef v8::Handle<v8::String> JSString; | |
typedef v8::Local<v8::String> JSAccessorProperty; | |
typedef const v8::PropertyCallbackInfo<v8::Value>& JSAccessorInfo; | |
typedef v8::Local<v8::Value> JSValue | |
enum JSType{Int = 1, Uint, String, Number, Bool, Array, Object} | |
class JSAccessor{ | |
public: | |
JSString name; | |
void (*Getter)(JSAccessorProperty, JSAccessorInfo); | |
void (*Setter)(JSAccessorProperty, JSValue, JSAccessorInfo); | |
} | |
template<class T> class JSObj{ | |
public: | |
JSObj(){ | |
Template = v8::FunctionTemplate::New(v8::Isolate::GetCurrent());\ | |
InstanceTemplate = Template->InstanceTemplate(); | |
Prototype = Template->PrototypeTemplate(); | |
Constructor = Template->GetFunction(); | |
InstanceTemplate->SetInternalFieldCount(2); | |
accessors = NULL; | |
} | |
~JSObj(){ | |
Template.Clear(); | |
InstanceTemplate.Clear(); | |
Prototype.Clear(); | |
if(!TypeName->IsEmpty()) | |
TypeName->Clear(); | |
} | |
JSString TypeName; | |
void (*Finalize)(const v8::WeakCallbackData<v8::Object, T> &args); | |
JSAccessor *accessors; //Null-terminated. | |
unsigned long long ID; | |
v8::Handle<v8::FunctionTemplate> Template; | |
v8::Handle<v8::ObjectTemplate> InstanceTemplate; | |
v8::Handle<v8::ObjectTemplate> Prototype; | |
v8::Handle<v8::Function> Constructor; | |
} | |
///// | |
// Data Validation | |
namespace CheckArg{ | |
inline bool Int(JAArguments args, int i){ | |
if (!args[i]->IsString()) { | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), name))); | |
return false; | |
} | |
return true; | |
} | |
inline bool CheckSig(JSArguments args, int num, int *argtypes/*<- Null terminated*/){ | |
const char *prevF = NULL; | |
auto iso = v8::Isolate::GetCurrent(), | |
if(args.Length()<num) | |
return false; | |
unsigned char argnumprnt [2] = {'0', '\0'}; | |
for(int i = 0; i<num; i++){ | |
*argnumprnt = (unsigned char)'0'+(unsigned char)i; | |
assert(argtypes[i]!=NULL); | |
switch(argtypes[i]){ | |
case JSType::String: | |
if (args[i]->IsString()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not a String."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str()));\ | |
return; | |
case JSType::Int: | |
if (args[i]->IsInt32()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not an Integer."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
case JSType::Uint: | |
if (args[i]->IsUint32()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not an Integer."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
case JSType::Number: | |
if (args[i]->IsNumber()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not a Number."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
case JSType::Bool: | |
if (args[i]->IsBoolean()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not a Boolean."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
case JSType::Array: | |
if (args[i]->IsArray()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is not an Array."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
case JSType::Object: | |
if (!args[i]->IsUndefined()) | |
break; | |
TS_Stack_PreviousFunctionName(prevF); | |
std::string err = std::string("[" PLUGINNAME "] ").append(prevF).append(" Error: Argument ").append(argnumprnt).append(" is undefined."); | |
args.GetReturnValue().Set( v8::Exception::TypeError(v8::String::NewFromUtf8(iso, err.c_str())); | |
return; | |
} | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
// Wrapping Objects | |
template<class T> inline v8::Handle<v8::Value> WrapObject(const JSObj<T> &JSo, const T& obj){ | |
// | |
auto iso = v8::Isolate::GetCurrent(); | |
///// | |
// Create a JS object that holds an ID number and a pointer to the object | |
v8::Handle<v8::Object> returnobj = JSo.Constructor->NewInstance(); | |
returnobj->SetAlignedPointerInInternalField(0, &obj); | |
returnobj->SetInternalField(1, v8::Integer::NewFromUnsigned(iso, JSo.ID)); | |
returnobj->GetConstructorName()=JSo.TypeName); | |
///// | |
// Set the weak reference callback | |
v8::Persistent<v8::Object> preturnobj(iso, returnobj); | |
preturnobj.SetWeak(&obj, JSo.Finalize); | |
// | |
return preturnobj; | |
} | |
}; | |
/////////////////////////////////////////////////////////////////////////////// | |
// Legacy and Old-fashioned stuff | |
///// | |
// Memory alignment | |
#define MIN_MEM_ALIGN 2 | |
#ifdef _MSC_VER | |
#define __func__ __FUNCTION__ | |
#define MEMALIGN(X) _declspec(align(X)) | |
#else | |
#define MEMALIGN(X) __attribute__((aligned(X))) | |
#endif | |
#define MINMEMALIGN MEMALIGN(MIN_MEM_ALIGN) | |
#ifdef NOPLUGNAME | |
#undef PLUGINNAME | |
#endif | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment