-
-
Save gocarlos/0c6783646074144b3af008b54a379d07 to your computer and use it in GitHub Desktop.
JerryScript++
This file contains hidden or 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
#include <string> | |
#include <vector> | |
#include "jerryscript.h" | |
namespace JerryScript { | |
class Value; | |
class Undefined; | |
class Null; | |
class Boolean; | |
class Object; | |
class Number; | |
class String; | |
class Function; | |
class Promise; | |
class RegExp; | |
class Error; | |
class Abort; | |
class Symbol; | |
class ArrayBuffer; | |
class DataView; | |
class TypedArray; | |
class Property; | |
class Context; | |
typedef Value (*jerry_cxx_handler_t)(Context*); | |
/** | |
* @brief Defines a wrapper for `jerry_value_t`. | |
* @note This class is intended to represent the most low-level type. | |
* Therefore, it is also considered the base class for any other type. | |
*/ | |
class Value { | |
private: | |
/** | |
* @brief the jerry_value_t that is being worked on. | |
*/ | |
jerry_value_t value; | |
public: | |
/** | |
* @brief All this method does is call jerry_release_value(). | |
*/ | |
~Value(); | |
// Strings | |
Value(jerry_char_t*, jerry_size_t); | |
Value(std::string); | |
// From other value | |
Value(Value[]); | |
Value(Value[], jerry_size_t); | |
Value(jerry_value_t[]); | |
jerry_type_t typeof(); | |
void aquire(); | |
void release(); | |
// Accessing the native struct | |
jerry_value_t operator -> (); | |
jerry_value_t raw(); | |
// Stringification | |
virtual std::string toString(); | |
virtual jerry_char_t* toJerryString(); | |
virtual operator std::string (); | |
virtual operator jerry_char_t* (); | |
// Property stuff | |
virtual operator Undefined (); | |
virtual operator Null (); | |
virtual operator Boolean (); | |
virtual operator Object (); | |
virtual operator Number (); | |
virtual operator String (); | |
virtual operator Function (); | |
virtual operator Promise (); | |
virtual operator RegExp (); | |
virtual operator Error (); | |
virtual operator Abort (); | |
virtual operator Symbol (); | |
virtual operator ArrayBuffer (); | |
virtual operator DataView (); | |
virtual operator TypedArray (); | |
// TODO: implement type verification (isNull, isFunction, ...) | |
}; | |
class Undefined: public Value { | |
public: | |
Undefined(); | |
}; | |
class Null: public Value { | |
public: | |
Null(); | |
}; | |
class Object: public Value { | |
public: | |
/** | |
* @brief Implements Object.create(Object) | |
*/ | |
static Object create(Object); | |
/** | |
* @brief Constructs an Object representing the global object. | |
*/ | |
static Object global(); | |
// Element access and setter | |
Value operator [] (Number); | |
//Value operator [] (Number, Value); | |
Value operator [] (String); | |
//Value operator [] (String, Value); | |
// TODO: Some of those should be static... | |
static Boolean __defineGetter__(Object, Number, Function); | |
static Boolean __defineGetter__(Object, String, Function); | |
static Boolean __defineSetter__(Object, Number, Function); | |
static Boolean __defineSetter__(Object, String, Function); | |
static Function __lookupGetter__(Object, Number); | |
static Function __lookupGetter__(Object, String); | |
static Function __lookupSetter__(Object, Number); | |
static Function __lookupSetter__(Object, String); | |
Boolean hasProperty(Number); | |
Boolean hasProperty(String); | |
Boolean hasOwnProperty(Number); | |
Boolean hasOwnProperty(String); | |
Boolean deleteProperty(Number); | |
Boolean deleteProperty(String); | |
Boolean deletePropertyByIndex(Number); | |
static Boolean isPrototypeOf(Value); | |
Value getPrototype(); | |
Value setPrototype(Value); | |
Boolean propertyIsEnumerable(Number); | |
Boolean propertyIsEnumerable(String); | |
Property getProperty(Number); | |
Property getProperty(String); | |
Object setProperty(Number, Value); | |
Object setProperty(String, Value); | |
Object setProperty(Property); | |
static Array keys(Object); | |
virtual Value valueOf(); | |
uint32_t length(); | |
/* | |
TODO: I am sure there is a more C++ friendly way for this... | |
I.e.: Create Object::NativePointer that can be extended and features a virtual method | |
which is called. Would allow one to pass existing classes that just inherit their base | |
and have it automatically done. Might actually just call the destructor, though... | |
*/ | |
void setNativePointer(void*, jerry_object_native_info_t); | |
bool getNativePointer(void**, jerry_object_native_info_t*); | |
bool deleteNativePointer(jerry_object_native_info_t*); | |
}; | |
class Number: public Object { | |
public: | |
// TODO: more integer types. We want them all! | |
Number(int); | |
Number(double); | |
Number(float); | |
Number(long); | |
Number(jerry_size_t); | |
Number(jerry_length_t); | |
// FIXME: Properly implement ALL prototype and instance methods. | |
// These ones are taken off NodeJS. | |
static Number EPSILON(); | |
static Number MAX_SAFE_INTEGER(); | |
static Number MAX_VALUE(); | |
static Number MIN_SAFE_INTEGER(); | |
static Number MIN_VALUE(); | |
static Number NEGATIVE_INFINITY(); | |
static Number NaN(); | |
static Number POSITIVE_INFINITY(); | |
static Boolean isNaN(Number); | |
static Boolean isFinite(Number); | |
static Boolean isInfinite(Number); | |
static Boolean isInteger(Number); | |
static Boolean isSafeInteger(Number); | |
static Number parseFloat(Value); | |
static Number parseInt(Value); | |
// TODO: Define parameters | |
Number toExponential(); | |
Number toFixed(); | |
Number toPrecision(); | |
// Syntax shuggar for C++ | |
Number operator + (Number); | |
Number operator - (Number); | |
Number operator * (Number); | |
Number operator / (Number); | |
Number operator % (Number); | |
}; | |
class Array: public Object { | |
public: | |
/** | |
* @brief Creates an array off an object or array. | |
*/ | |
static Array from(Value); | |
/** | |
* @brief Create a blank, dynamic array. | |
*/ | |
Array(); | |
/** | |
* @brief Create an array with pre-defined length. | |
*/ | |
Array(Number); | |
// FIXME: Implement all prototype and instance methods. | |
// TODO: Implement iteration support. | |
Value concat(); | |
Value every(); | |
Value fill(); | |
Value filter(); | |
Value find(); | |
Value findIndex(); | |
Value flat(); | |
Value flatMap(); | |
Value forEach(); | |
Boolean includes(); | |
Number indexOf(); | |
Value join(); | |
Array keys(); | |
Value lastIndexOf(); | |
Value map(); | |
Value pop(); | |
Value push(Value); | |
Value reduce(); | |
Value reduceRight(); | |
Value reverse(); | |
Value shift(); | |
Value slice(); | |
Value some(); | |
Value sort(); | |
Value splice(); | |
Value unshift(); | |
uint32_t length(); | |
}; | |
class String: public Object { | |
public: | |
/** | |
* @brief Create a blank string. | |
*/ | |
String(); | |
/** | |
* @brief Create a new string off an existing JerryScript value. | |
*/ | |
String(jerry_char_t*); | |
/** | |
* @brief Create a new string off an existing JerryScript string, with fixed length. | |
*/ | |
String(jerry_char_t*, jerry_size_t); | |
/** | |
* @brief Create a new string off a C++ string. | |
*/ | |
String(std::string); | |
/** | |
* @brief Create a new String off a C-string. | |
* @note String must be NULL terminated. | |
*/ | |
String(const char*); | |
/** | |
* @brief Create a new string off a C-string, with fixed length. | |
* @note String musn't be NULL terminated. | |
*/ | |
String(const char*, int); | |
// FIXME: Add all prototype and instance methods. | |
// TODO: Add RegExp support. | |
static String fromCharCode(Number); | |
static String fromCodePoint(); | |
String charAt(Number); | |
Number charCodeAt(Number); | |
Number codePointAt(Number); | |
String concat(String, String); | |
String concat(String[]); | |
Boolean endsWith(String); | |
String fixed(); | |
Boolean includes(String); | |
Number indexOf(String); | |
Number lastIndexOf(String); | |
Boolean match(RegExp); | |
//Value normalize(); | |
String padEnd(); | |
String padStart(); | |
String repeat(String, Number); | |
String replace(String, String); | |
//String search(String); | |
String slice(Number); | |
Array split(String); | |
Boolean startsWith(String); | |
String substr(Number, Number); | |
//String substring(Number, Number); | |
String toLowerCase(); | |
String toUpperCase(); | |
String trim(); | |
String trimEnd(); | |
String trimLeft(); | |
String trimRight(); | |
String trimStart(); | |
}; | |
class Function: public Object { | |
private: | |
Value bound; | |
public: | |
/** | |
* @brief Create a new function off a C-based handler. | |
*/ | |
Function(jerry_external_handler_t); | |
/** | |
* @brief Create a new function off a C++-based handler. | |
*/ | |
Function(jerry_cxx_handler_t); | |
/** | |
* @brief Call a given function. The signature matches Function.call | |
* @note The second parameter must be NULL terminated. Probably should | |
* use a better type here... maybe std::vector<Value>? | |
*/ | |
Value call(Value, Value[]); | |
/** | |
* @brief Bind a "this" to the function. | |
*/ | |
void bind(Value); | |
}; | |
class Promise: public Object { | |
public: | |
Value reject(Value); | |
Value resolve(Value); | |
}; | |
class RegEx: public Object { | |
public: | |
RegEx(String, uint16_t); | |
RegEx(jerry_char_t*, uint16_t); | |
RegEx(jerry_char_t*, jerry_size_t, uint16_t); | |
RegEx(std::string, uint16_t); | |
uint16_t getFlags(); | |
Boolean isGlobal(); | |
Boolean isIgnoreCase(); | |
Boolean isMultiline(); | |
Object match(Value); | |
Boolean test(Value); | |
}; | |
/** | |
* @brief A JerryScript error. May also be thrown, when exceptions are available. | |
*/ | |
class Error: public Value /*, std::exception*/ { | |
public: | |
Error(); | |
Error(String); | |
Error(jerry_char_t*); | |
Error(jerry_char_t*, jerry_size_t); | |
Error(std::string); | |
Error(std::exception); | |
Value getValue(); | |
Error setValue(); | |
jerry_error_t type(); | |
String what(); | |
}; | |
/** | |
* @brief Critical, unrecoverable error. May be thrown. | |
*/ | |
class Abort: public Error { | |
private: | |
String message; | |
public: | |
Abort(); | |
Abort(String); | |
Abort(jerry_char_t*); | |
Abort(jerry_char_t*, jerry_size_t); | |
Abort(std::string); | |
Abort(std::exception); | |
Value getValue(); | |
Abort setValue(); | |
jerry_error_t type(); | |
String what(); | |
}; | |
class Symbol: public Value { | |
public: | |
Symbol(Value); | |
}; | |
class ArrayBuffer: public Object { | |
public: | |
ArrayBuffer(jerry_length_t); | |
ArrayBuffer(jerry_length_t, uint8_t*, jerry_object_native_free_callback_t); | |
jerry_length_t read(jerry_length_t, uint8_t*, jerry_length_t); | |
jerry_length_t write(jerry_length_t, uint8_t*, jerry_length_t); | |
jerry_length_t length(); | |
}; | |
class DataView: public Value { | |
public: | |
DataView(ArrayBuffer); | |
DataView(ArrayBuffer, jerry_length_t, jerry_length_t); | |
}; | |
class TypedArray: public Object { | |
public: | |
TypedArray(jerry_typedarray_type_t, jerry_length_t); | |
TypedArray(jerry_typedarray_type_t, ArrayBuffer); | |
TypedArray(jerry_typedarray_type_t, ArrayBuffer, jerry_length_t, jerry_length_t); | |
}; | |
class Property { | |
private: | |
jerry_property_descriptor_t prop; | |
public: | |
Property(); | |
~Property(); | |
String getName(); | |
Property setName(String); | |
Property setName(jerry_char_t*); | |
Property setName(jerry_char_t*, jerry_size_t); | |
Value getValue(); | |
Property setValue(Value); | |
Function getGetter(); | |
Property setGetter(Function); | |
Function getSetter(); | |
Property setSetter(Function); | |
Boolean isValueDefined(); | |
//Boolean setValueDefined(Boolean); | |
Boolean isGetDefined(); | |
//Boolean setGetDefined(Boolean); | |
Boolean isSetDefined(); | |
//Boolean setSetDefined(Boolean); | |
//Boolean isWritableDefined(); | |
//Boolean setWritableDefined(Boolean); | |
Boolean isWritable(); | |
Boolean setWritable(Boolean); | |
//Boolean isEnumerableDefined(); | |
//Boolean setEnumerableDefined(Boolean); | |
Boolean isEnumerable(); | |
Boolean setEnumerable(Boolean); | |
//Boolean isConfigurableDefined(); | |
//Boolean setConfigurableDefined(Boolean); | |
Boolean isConfigurable(); | |
Boolean setConfigurable(Boolean); | |
}; | |
/** | |
* @brief Represents the current context in a C++ based handler. | |
*/ | |
class Context { | |
private: | |
Function func_value; | |
Value this_value; | |
std::vector<Value> arguments; | |
public: | |
/** | |
* @brief Constructs a context off the 4 arguments passed to a C handler. | |
*/ | |
Context(jerry_value_t, jerry_value_t, jerry_value_t, jerry_value_t); | |
Value arg(jerry_size_t); | |
Value arg(Number); | |
Number args(); | |
Value self(); | |
Function func(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment