| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // Please see LICENSE.md in repository root for license information
- // https://github.com/AtomicGameEngine/AtomicGameEngine
- #include "JSAPI.h"
- #include "JSVM.h"
- /*
- // script can new an instance
- // script can get an engine ptr back (which may be to a previously script new'd instance)
- // script should always get the same JS Object back which means a lookup table (strong ref)
- // we can look directly at the tval (exposed from duktape internals) to see if the reference count is 1
- // script owned
- // new'd script side, script holds a RefCounted reference
- // native owned
- // native script classes not derived from refcounted
- // if not inherited from RefCounted can script access?
- // if so, script side will always need to delete?
- */
- namespace Atomic
- {
- void js_class_get_prototype(duk_context* ctx, const char* package, const char *classname)
- {
- duk_get_global_string(ctx, package);
- duk_get_prop_string(ctx, -1, classname);
- duk_get_prop_string(ctx, -1, "prototype");
- duk_remove(ctx, -2); // remove class object
- duk_remove(ctx, -2); // remove Atomic object
- }
- void js_class_get_constructor(duk_context* ctx, const char* package, const char *classname)
- {
- duk_get_global_string(ctx, package);
- duk_get_prop_string(ctx, -1, classname);
- duk_remove(ctx, -2); // remove package
- }
- void js_constructor_basecall(duk_context* ctx, const char* package, const char* baseclass)
- {
- int top = duk_get_top(ctx);
- duk_get_global_string(ctx, package);
- duk_get_prop_string(ctx, -1, baseclass);
- assert(duk_is_function(ctx, -1));
- duk_push_this(ctx);
- duk_call_method(ctx, 0);
- duk_pop_n(ctx, 2);
- assert (top == duk_get_top(ctx));
- }
- void js_class_declare_internal(JSVM* vm, void* uniqueClassID, const char* package, const char* classname, duk_c_function constructor)
- {
- duk_context* ctx = vm->GetJSContext();
- // stash a lookup from the uniqueID to the package name
- // (NULL) == non-object, so core "Atomic" package
- if (uniqueClassID)
- {
- duk_push_heap_stash(ctx);
- duk_push_pointer(ctx, uniqueClassID);
- duk_push_string(ctx, package);
- duk_put_prop(ctx, -3);
- duk_pop(ctx);
- }
- else
- {
- // RefCounted only supported in Atomic package
- assert(String("Atomic") == package );
- }
- duk_get_global_string(ctx, package);
- duk_push_c_function(ctx, constructor, DUK_VARARGS);
- duk_put_prop_string(ctx, -2, classname);
- duk_pop(ctx);
- }
- void js_class_push_propertyobject(JSVM* vm, const char* package, const char* classname)
- {
- duk_context* ctx = vm->GetJSContext();
- String pname;
- pname.AppendWithFormat("__%s__Properties", classname);
- duk_get_global_string(ctx, package);
- duk_push_object(ctx);
- duk_dup(ctx, -1);
- duk_put_prop_string(ctx, -3, pname.CString());
- duk_remove(ctx, -2); // remove Atomic object
- }
- void js_setup_prototype(JSVM* vm, const char* package, const char* classname, const char* basePackage, const char* basename, bool hasProperties)
- {
- duk_context* ctx = vm->GetJSContext();
- String pname;
- pname.AppendWithFormat("__%s__Properties", classname);
- int top = duk_get_top(ctx);
- duk_get_global_string(ctx,package);
- duk_get_prop_string(ctx, -1, classname);
- assert(duk_is_c_function(ctx, -1));
- if (!strlen(basename))
- {
- // prototype
- duk_push_object(ctx);
- duk_dup(ctx, -2); // AObject constructor function
- duk_put_prop_string(ctx, -2, "constructor");
- duk_put_prop_string(ctx, -2, "prototype");
- duk_pop_n(ctx, 2);
- assert (top == duk_get_top(ctx));
- return;
- }
- // prototype
- duk_get_global_string(ctx, "Object");
- duk_get_prop_string(ctx, -1, "create");
- assert(duk_is_function(ctx, -1));
- duk_remove(ctx, -2); // remove Object
- duk_get_global_string(ctx, basePackage);
- duk_get_prop_string(ctx, -1, basename);
- assert(duk_is_function(ctx, -1));
- duk_get_prop_string(ctx, -1, "prototype");
- assert(duk_is_object(ctx, -1));
- duk_remove(ctx, -2); // remove basename
- int numargs = 1;
- if (hasProperties)
- {
- duk_get_global_string(ctx, package);
- duk_get_prop_string(ctx, -1, pname.CString());
- assert(duk_is_object(ctx, -1));
- duk_remove(ctx, -2);
- duk_remove(ctx, -3); // remove package
- numargs++;
- }
- else
- duk_remove(ctx, -2); // remove package
- duk_call(ctx, numargs);
- assert(duk_is_object(ctx, -1));
- duk_dup(ctx, -2);
- duk_put_prop_string(ctx, -2, "constructor");
- //duk_dup(ctx, -1);
- duk_put_prop_string(ctx, -2, "prototype");
- // pop the classname object
- duk_pop(ctx);
- // pop the Atomic Object
- duk_pop(ctx);
- assert (top == duk_get_top(ctx));
- }
- void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
- {
- v.Clear();
- duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
- while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
- /* [ ... enum key ] */
- const char* key = duk_to_string(ctx, -2);
- if (duk_is_number(ctx, -1)) {
- v[key] = (float) duk_to_number(ctx, -1);
- } else if (duk_is_boolean(ctx, -1)) {
- v[key] = duk_to_boolean(ctx, -1) ? true : false;
- }
- else if (duk_is_string(ctx, -1)) {
- v[key] = duk_to_string(ctx, -1);
- } else if (duk_get_heapptr(ctx, -1)) {
- v[key] = js_to_class_instance<Object>(ctx, -1, 0);
- }
- duk_pop_2(ctx); /* pop_key & value*/
- }
- duk_pop(ctx); /* pop enum object */
- }
- void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
- {
- v.Clear();
- // convert to abs index
- if (variantIdx < 0)
- variantIdx = duk_get_top(ctx) + variantIdx;
- if (duk_is_boolean(ctx, variantIdx))
- {
- v = duk_to_boolean(ctx, variantIdx) ? true : false;
- return;
- }
- if (duk_is_string(ctx, variantIdx))
- {
- v = duk_to_string(ctx, variantIdx);
- return;
- }
- if (duk_is_number(ctx, variantIdx))
- {
- v = (float) duk_to_number(ctx, variantIdx);
- return;
- }
- if (duk_is_pointer(ctx, variantIdx))
- {
- v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
- return;
- }
- if (duk_is_array(ctx, variantIdx))
- {
- if (duk_get_length(ctx, variantIdx) == 2)
- {
- Vector2 v2;
- duk_get_prop_index(ctx, variantIdx, 0);
- v2.x_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 1);
- v2.y_ = duk_to_number(ctx, -1);
- duk_pop_n(ctx, 2);
- v = v2;
- return;
- }
- else if (duk_get_length(ctx, variantIdx) == 3)
- {
- Vector3 v3;
- duk_get_prop_index(ctx, variantIdx, 0);
- v3.x_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 1);
- v3.y_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 2);
- v3.z_ = duk_to_number(ctx, -1);
- duk_pop_n(ctx, 3);
- v = v3;
- return;
- }
- else if (duk_get_length(ctx, variantIdx) == 4)
- {
- Vector4 v4;
- duk_get_prop_index(ctx, variantIdx, 0);
- v4.x_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 1);
- v4.y_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 2);
- v4.z_ = duk_to_number(ctx, -1);
- duk_get_prop_index(ctx, variantIdx, 3);
- v4.w_ = duk_to_number(ctx, -1);
- duk_pop_n(ctx, 4);
- v = v4;
- return;
- }
- return;
- }
- }
- // variant map Proxy getter, so we can convert access to string based
- // member lookup, to string hash on the fly
- static int variantmap_property_get(duk_context* ctx)
- {
- // targ, key, recv
- if (duk_is_string(ctx, 1))
- {
- StringHash key = duk_to_string(ctx, 1);
- duk_get_prop_index(ctx, 0, (unsigned) key.Value());
- return 1;
- }
- duk_push_undefined(ctx);
- return 1;
- }
- // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
- // see (lengthy) note in JSEventDispatcher::EndSendEvent
- static int variantmap_property_deleteproperty(duk_context* ctx)
- {
- // deleteProperty: function (targ, key)
- duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
- while (duk_next(ctx, -1, 0)) {
- duk_del_prop(ctx, 0);
- }
- duk_push_boolean(ctx, 1);
- return 1;
- }
- void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
- {
- // setup proxy so we can map string
- duk_get_global_string(ctx, "Proxy");
- duk_push_object(ctx);
- VariantMap::ConstIterator itr = vmap.Begin();
- while (itr != vmap.End()) {
- js_push_variant(ctx, itr->second_);
- if (duk_is_undefined(ctx, -1)) {
- duk_pop(ctx);
- }
- else
- {
- duk_put_prop_index(ctx, -2, (unsigned) itr->first_.Value());
- }
- itr++;
- }
- // setup property handler
- duk_push_object(ctx);
- duk_push_c_function(ctx, variantmap_property_get, 3);
- duk_put_prop_string(ctx, -2, "get");
- duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
- duk_put_prop_string(ctx, -2, "deleteProperty");
- duk_new(ctx, 2);
- }
- void js_push_variant(duk_context *ctx, const Variant& v)
- {
- VariantType type = v.GetType();
- RefCounted* ref;
- Object* object;
- Vector2 vector2 = Vector2::ZERO;
- Vector3 vector3 = Vector3::ZERO;
- Vector4 vector4 = Vector4::ZERO;
- Color color = Color::BLACK;
- void* uniqueClassID = NULL;
- const char* package = NULL;
- switch (type)
- {
- case VAR_NONE:
- duk_push_undefined(ctx);
- break;
- case VAR_VOIDPTR:
- duk_push_null(ctx);
- break;
- case VAR_PTR:
- ref = v.GetPtr();
- if (!ref || !ref->IsObject() || !ref->Refs())
- {
- duk_push_null(ctx);
- break;
- }
- object = (Object*) ref;
- // check that class is supported
- uniqueClassID = (void *) object->GetTypeName().CString();
- duk_push_heap_stash(ctx);
- duk_push_pointer(ctx, uniqueClassID);
- duk_get_prop(ctx, -2);
- package = duk_to_string(ctx, -1);
- duk_pop_2(ctx);
- // check that class is supported
- duk_get_global_string(ctx, package);
- // will not handle renamed classes!
- duk_get_prop_string(ctx, -1, object->GetTypeName().CString());
- if (!duk_is_function(ctx, -1))
- object = NULL;
- duk_pop_n(ctx, 2);
- if (object)
- js_push_class_object_instance(ctx, object);
- else
- duk_push_undefined(ctx);
- break;
- case VAR_BOOL:
- duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
- break;
- case VAR_INT:
- duk_push_number(ctx, v.GetInt());
- break;
- case VAR_FLOAT:
- duk_push_number(ctx, v.GetFloat());
- break;
- case VAR_STRING:
- duk_push_string(ctx, v.GetString().CString());
- break;
- case VAR_VECTOR2:
- vector2 = v.GetVector2();
- duk_push_array(ctx);
- duk_push_number(ctx, vector2.x_);
- duk_put_prop_index(ctx, -2, 0);
- duk_push_number(ctx, vector2.y_);
- duk_put_prop_index(ctx, -2, 1);
- break;
- case VAR_VECTOR3:
- vector3 = v.GetVector3();
- duk_push_array(ctx);
- duk_push_number(ctx, vector3.x_);
- duk_put_prop_index(ctx, -2, 0);
- duk_push_number(ctx, vector3.y_);
- duk_put_prop_index(ctx, -2, 1);
- duk_push_number(ctx, vector3.z_);
- duk_put_prop_index(ctx, -2, 2);
- break;
- case VAR_QUATERNION:
- vector3 = v.GetQuaternion().EulerAngles();
- duk_push_array(ctx);
- duk_push_number(ctx, vector3.x_);
- duk_put_prop_index(ctx, -2, 0);
- duk_push_number(ctx, vector3.y_);
- duk_put_prop_index(ctx, -2, 1);
- duk_push_number(ctx, vector3.z_);
- duk_put_prop_index(ctx, -2, 2);
- break;
- case VAR_COLOR:
- color = v.GetColor();
- duk_push_array(ctx);
- duk_push_number(ctx, color.r_);
- duk_put_prop_index(ctx, -2, 0);
- duk_push_number(ctx, color.g_);
- duk_put_prop_index(ctx, -2, 1);
- duk_push_number(ctx, color.b_);
- duk_put_prop_index(ctx, -2, 2);
- duk_push_number(ctx, color.a_);
- duk_put_prop_index(ctx, -2, 3);
- break;
- case VAR_VECTOR4:
- vector4 = v.GetVector4();
- duk_push_array(ctx);
- duk_push_number(ctx, vector4.x_);
- duk_put_prop_index(ctx, -2, 0);
- duk_push_number(ctx, vector4.y_);
- duk_put_prop_index(ctx, -2, 1);
- duk_push_number(ctx, vector4.z_);
- duk_put_prop_index(ctx, -2, 2);
- duk_push_number(ctx, vector4.w_);
- duk_put_prop_index(ctx, -2, 3);
- break;
- default:
- duk_push_undefined(ctx);
- break;
- }
- }
- }
|