| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- #include <Atomic/IO/FileSystem.h>
- #include "JSBind.h"
- #include "JSBModule.h"
- #include "JSBPackage.h"
- #include "JSBEnum.h"
- #include "JSBClass.h"
- #include "JSBFunction.h"
- #include "JSBFunctionWriter.h"
- namespace ToolCore
- {
- JSBFunctionWriter::JSBFunctionWriter(JSBFunction *function) : function_(function)
- {
- }
- void JSBFunctionWriter::WriteParameterMarshal(String& source)
- {
- // generate args
- Vector<JSBFunctionType*>& parameters = function_->GetParameters();
- int cparam = 0;
- if (parameters.Size())
- {
- for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
- {
- JSBFunctionType * ptype = parameters.At(i);
- // ignore "Context" parameters
- if (ptype->type_->asClassType())
- {
- JSBClassType* classType = ptype->type_->asClassType();
- JSBClass* klass = classType->class_;
- if (klass->GetName() == "Context")
- {
- cparam--;
- continue;
- }
- }
- String pstring = ptype->ToArgString(cparam);
- const String& init = ptype->initializer_;
- if (ptype->type_->asClassType())
- {
- JSBClassType* classType = ptype->type_->asClassType();
- JSBClass* klass = classType->class_;
- if (!klass->IsNumberArray())
- {
- if (init.Length())
- {
- source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? js_to_class_instance<%s>(ctx, %i, 0) : %s;\n",
- pstring.CString(), cparam + 1, klass->GetNativeName().CString(), cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("%s = js_to_class_instance<%s>(ctx, %i, 0);\n",
- pstring.CString(), klass->GetNativeName().CString(), cparam);
- }
- }
- else
- {
- int elements = klass->GetNumberArrayElements();
- String elementType = klass->GetArrayElementType();
- source.AppendWithFormat("%s arrayData%i[%i];\n", elementType.CString(), cparam, elements);
- if (init.Length())
- {
- source.AppendWithFormat("const %s& defaultArg%i = %s;\n", klass->GetNativeName().CString(), cparam, init.CString());
- source.AppendWithFormat("if (duk_get_top(ctx) >= %i) {\n", cparam + 1);
- }
- for (int j = 0; j < elements; j++)
- {
- source.AppendWithFormat("duk_get_prop_index(ctx, %i, %i);\n", cparam, j);
- source.AppendWithFormat("arrayData%i[%i] = (%s) duk_to_number(ctx, -1);\n", cparam, j, elementType.CString());
- }
- source.AppendWithFormat("duk_pop_n(ctx, %i);\n", elements);
- if (init.Length())
- {
- source.Append("}\n");
- source.AppendWithFormat("%s __arg%i(duk_get_top(ctx) >= %i ? arrayData%i : defaultArg%i);\n",
- klass->GetNativeName().CString(), cparam, cparam + 1, cparam, cparam);
- }
- else
- {
- source.AppendWithFormat("%s __arg%i(arrayData%i);\n", klass->GetNativeName().CString(), cparam, cparam);
- }
- }
- }
- else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
- {
- if (init.Length())
- {
- source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_to_string(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("%s = duk_to_string(ctx, %i);\n", pstring.CString(), cparam);
- }
- }
- else if (ptype->type_->asHeapPtrType())
- {
- if (init.Length())
- {
- source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_get_heapptr(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("%s = duk_get_heapptr(ctx, %i);\n", pstring.CString(), cparam);
- }
- }
- else if (ptype->type_->asPrimitiveType())
- {
- JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
- if (prtype->kind_ == JSBPrimitiveType::Bool)
- {
- if (init.Length())
- {
- source.AppendWithFormat("bool __arg%i = duk_get_top(ctx) >= %i ? (duk_to_boolean(ctx, %i) ? true : false) : %s;\n",
- cparam, cparam + 1, cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("bool __arg%i = duk_to_boolean(ctx, %i) ? true : false;\n", cparam, cparam);
- }
- }
- else
- {
- if (init.Length())
- {
- source.AppendWithFormat("double __arg%i = duk_get_top(ctx) >= %i ? (duk_to_number(ctx, %i)) : %s;\n",
- cparam, cparam + 1, cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("double __arg%i = duk_to_number(ctx, %i);\n", cparam, cparam);
- }
- }
- }
- else if (ptype->type_->asEnumType())
- {
- JSBEnumType* etype = ptype->type_->asEnumType();
- if (init.Length())
- {
- source.AppendWithFormat("%s __arg%i = duk_get_top(ctx) >= %i ? ((%s) ((int) duk_to_number(ctx, %i))) : %s;\n", etype->enum_->GetName().CString(),
- cparam, cparam + 1, etype->enum_->GetName().CString(), cparam, init.CString());
- }
- else
- {
- source.AppendWithFormat("%s __arg%i = (%s) ((int)duk_to_number(ctx, %i));\n", etype->enum_->GetName().CString(),
- cparam, etype->enum_->GetName().CString(), cparam);
- }
- }
- }
- }
- }
- void JSBFunctionWriter::WriteConstructor(String& source)
- {
- // TODO: refactor this
- if (function_->name_ == "RefCounted")
- {
- source.Append("// finalizer may be called more than once\n" \
- "static int jsb_finalizer_RefCounted(duk_context *ctx)\n" \
- "{\n" \
- "JSVM* vm = JSVM::GetJSVM(ctx);\n" \
- \
- "duk_get_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
- \
- "if (!duk_is_boolean(ctx, -1))\n" \
- "{\n" \
- "RefCounted* ref = vm->GetObjectPtr(duk_get_heapptr(ctx, 0));\n" \
- "vm->RemoveObject(ref);\n" \
- "ref->ReleaseRef();\n" \
- "duk_push_boolean(ctx, 1);\n" \
- "duk_put_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
- "}\n" \
- \
- "return 0;\n" \
- "}\n");
- }
- JSBClass* klass = function_->class_;
- JSBClass* base = klass->GetBaseClass();
- // Constructor
- source.AppendWithFormat("duk_ret_t jsb_constructor_%s(duk_context* ctx)\n{\n", klass->GetName().CString());
- source.Append(" if (duk_is_constructor_call(ctx))\n {\n");
- if (!klass->IsAbstract() && !klass->IsNumberArray())
- {
- String marshal;
- WriteParameterMarshal(marshal);
- String sparams;
- int cparam = 0;
- Vector<JSBFunctionType*>& parameters = function_->GetParameters();
- for (unsigned i = 0; i < parameters.Size(); i++, cparam++)
- {
- JSBFunctionType * ptype = parameters.At(i);
- String sarg;
- if (ptype->type_->asClassType())
- {
- JSBClassType* classType = ptype->type_->asClassType();
- JSBClass* klass = classType->class_;
- if (klass->GetName() == "Context")
- {
- sarg = "JSVM::GetJSVM(ctx)->GetContext()";
- cparam--;
- }
- }
- if (!sarg.Length())
- {
- sarg.AppendWithFormat("__arg%i", cparam);
- }
- sparams += sarg;
- if (i + 1 < parameters.Size())
- sparams += ", ";
- }
- source.AppendWithFormat("if (!duk_get_top(ctx) || !duk_is_pointer(ctx, 0))\n"\
- "{\n"\
- "%s\n"\
- "%s* native = new %s(%s);\n" \
- "duk_push_this(ctx);\n" \
- "JSVM::GetJSVM(ctx)->AddObject(duk_get_heapptr(ctx, -1), native);\n"\
- "duk_pop(ctx);\n"\
- "}\n" \
- "else if (duk_is_pointer(ctx, 0))\n" \
- "{\n" \
- "duk_push_this(ctx);\n" \
- "JSVM::GetJSVM(ctx)->AddObject(duk_get_heapptr(ctx, -1), (RefCounted*) duk_get_pointer(ctx, 0));\n" \
- "duk_pop(ctx);\n" \
- "}\n", marshal.CString(), klass->GetNativeName().CString(), klass->GetNativeName().CString(), sparams.CString());
- }
- else
- {
- if (klass->IsAbstract())
- source.Append("assert(0); // abstract class new'd\n");
- if (klass->IsNumberArray())
- source.Append("assert(0); // number array class new'd\n");
- }
- source.Append(" }\n");
- if (base)
- {
- source.AppendWithFormat(" js_constructor_basecall(ctx, \"%s\");\n", base->GetName().CString());
- }
- if (function_->name_ == "RefCounted")
- {
- source.Append("duk_push_this(ctx);\n "\
- "duk_push_c_function(ctx, jsb_finalizer_RefCounted, 1);\n "\
- "duk_set_finalizer(ctx, -2);\n "\
- \
- "RefCounted* ref = JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, -1));\n "\
- "ref->AddRef();\n "\
- \
- "duk_pop(ctx);\n");
- }
- source += " return 0;";
- source += "\n}\n";
- }
- void JSBFunctionWriter::WriteFunction(String& source)
- {
- JSBClass* klass = function_->class_;
- source.AppendWithFormat("static int jsb_class_%s_%s(duk_context* ctx)\n{\n", klass->GetName().CString(), function_->name_.CString());
- WriteParameterMarshal(source);
- source.Append("duk_push_this(ctx);\n");
- source.AppendWithFormat("%s* native = js_to_class_instance<%s>(ctx, -1, 0);\n", klass->GetNativeName().CString(), klass->GetNativeName().CString());
- // declare return value;
- bool returnDeclared = false;
- JSBFunctionType* returnType = function_->returnType_;
- if (returnType)
- {
- if (returnType->type_->asStringType())
- {
- returnDeclared = true;
- source.Append("const String& retValue = ");
- }
- else if (returnType->type_->asPrimitiveType())
- {
- returnDeclared = true;
- JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
- if (prtype->kind_ == JSBPrimitiveType::Bool)
- {
- source.Append("bool retValue = ");
- }
- else
- {
- source.Append("double retValue = ");
- }
- }
- else if (returnType->type_->asClassType())
- {
- JSBClassType* klassType = returnType->type_->asClassType();
- if (returnType->isTemplate_)
- {
- returnDeclared = true;
- source.AppendWithFormat("SharedPtr<%s> object = ", klassType->class_->GetNativeName().CString());
- }
- else if (klassType->class_->IsObject())
- {
- returnDeclared = true;
- source.Append("const Object* object = ");
- }
- else if (klassType->class_->IsNumberArray())
- {
- returnDeclared = true;
- source.AppendWithFormat("const %s& retValue = ", klassType->class_->GetName().CString());
- }
- else
- {
- returnDeclared = true;
- source.Append("const RefCounted* object = ");
- }
- }
- else if (returnType->type_->asEnumType())
- {
- JSBEnumType* enumType = returnType->type_->asEnumType();
- returnDeclared = true;
- source.AppendWithFormat("%s retValue = ", enumType->enum_->GetName().CString());
- }
- }
- source.AppendWithFormat("native->%s(", function_->name_.CString());
- Vector<JSBFunctionType*>& parameters = function_->GetParameters();
- for (unsigned int i = 0; i < parameters.Size(); i++)
- {
- source.AppendWithFormat("__arg%i", i);
- if (i != parameters.Size() - 1)
- {
- source += ", ";
- }
- }
- source += ");\n";
- if (returnDeclared)
- {
- if (returnType->type_->asStringType())
- {
- source.Append("duk_push_string(ctx, retValue.CString());\n");
- }
- else if (returnType->type_->asPrimitiveType())
- {
- JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
- if (prtype->kind_ == JSBPrimitiveType::Bool)
- {
- source.Append("duk_push_boolean(ctx, retValue ? 1 : 0);\n");
- }
- else
- {
- source.Append("duk_push_number(ctx, retValue);\n");
- }
- }
- else if (returnType->type_->asClassType())
- {
- JSBClassType* klassType = returnType->type_->asClassType();
- if (klassType->class_->IsObject())
- {
- returnDeclared = true;
- source.Append("js_push_class_object_instance(ctx, object);\n");
- }
- else if (klassType->class_->IsNumberArray())
- {
- returnDeclared = true;
- String elementType = klassType->class_->GetArrayElementType();
- source.AppendWithFormat("const %s* arrayData = retValue.Data();\n", elementType.CString());
- source.Append("duk_push_array(ctx);\n");
- for (int i = 0; i < klassType->class_->GetNumberArrayElements(); i++)
- {
- source.AppendWithFormat("duk_push_number(ctx, arrayData[%i]);\n", i);
- source.AppendWithFormat("duk_put_prop_index(ctx, -2, %i);\n", i);
- }
- }
- else
- {
- returnDeclared = true;
- source.AppendWithFormat("js_push_class_object_instance(ctx, object, \"%s\");\n", klassType->class_->GetName().CString());
- }
- }
- else if (returnType->type_->asEnumType())
- {
- returnDeclared = true;
- source.Append("duk_push_number(ctx, (double) retValue);\n");
- }
- source += "return 1;\n";
- }
- else
- {
- source += "return 0;\n";
- }
- source.Append("}\n");
- }
- void JSBFunctionWriter::GenerateSource(String& sourceOut)
- {
- String source = "";
- if (function_->IsConstructor())
- {
- WriteConstructor(source);
- }
- else
- {
- WriteFunction(source);
- }
- sourceOut += source;
- }
- }
|