Browse Source

Javascript HttpRequests, initial support for JSBind to handle Vector parameters (read-only)

Josh Engebretson 10 years ago
parent
commit
10eeb07c1e

+ 3 - 3
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/main.ts

@@ -1,6 +1,6 @@
-/// <reference path="/Users/josh/Dev/thunderbeast/AtomicGameEngine/Bin/Atomic.d.ts" />
-/// <reference path="/Users/josh/Dev/thunderbeast/AtomicGameEngine/Bin/ToolCore.d.ts" />
-/// <reference path="/Users/josh/Dev/thunderbeast/AtomicGameEngine/Bin/Editor.d.ts" />
+/// <reference path="/Users/josh/Dev/atomic/AtomicGameEngine/Bin/Atomic.d.ts" />
+/// <reference path="/Users/josh/Dev/atomic/AtomicGameEngine/Bin/ToolCore.d.ts" />
+/// <reference path="/Users/josh/Dev/atomic/AtomicGameEngine/Bin/Editor.d.ts" />
 
 /// <reference path="./AtomicWork.d.ts" />
 

+ 24 - 0
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -11,6 +11,10 @@
 #include <Atomic/Graphics/Graphics.h>
 #include <Atomic/Engine/Engine.h>
 
+#ifdef ATOMIC_NETWORK
+#include <Atomic/Network/Network.h>
+#endif
+
 #include "JSEvents.h"
 #include "JSVM.h"
 #include "JSComponent.h"
@@ -21,6 +25,10 @@
 #include "JSUIAPI.h"
 #include "JSScene.h"
 
+#ifdef ATOMIC_NETWORK
+#include "JSNetwork.h"
+#endif
+
 #include "JSAtomicGame.h"
 #include "JSAtomic.h"
 
@@ -144,6 +152,14 @@ static int js_atomic_GetFileSystem(duk_context* ctx)
     return 1;
 }
 
+#ifdef ATOMIC_NETWORK
+static int js_atomic_GetNetwork(duk_context* ctx)
+{
+    JSVM* vm = JSVM::GetJSVM(ctx);
+    js_push_class_object_instance(ctx, vm->GetSubsystem<Network>());
+    return 1;
+}
+#endif
 
 static int js_atomic_script(duk_context* ctx)
 {
@@ -259,6 +275,9 @@ void jsapi_init_atomic(JSVM* vm)
     jsapi_init_core(vm);
     jsapi_init_filesystem(vm);
     jsapi_init_io(vm);
+#ifdef ATOMIC_NETWORK
+    jsapi_init_network(vm);
+#endif
     jsapi_init_graphics(vm);
     jsapi_init_ui(vm);
     jsapi_init_scene(vm);
@@ -317,6 +336,11 @@ void jsapi_init_atomic(JSVM* vm)
     duk_push_c_function(ctx, js_atomic_GetFileSystem, 0);
     duk_put_prop_string(ctx, -2, "getFileSystem");
 
+#ifdef ATOMIC_NETWORK
+    duk_push_c_function(ctx, js_atomic_GetNetwork, 0);
+    duk_put_prop_string(ctx, -2, "getNetwork");
+#endif
+
     duk_push_c_function(ctx, js_atomic_script, 1);
     duk_put_prop_string(ctx, -2, "script");
 

+ 52 - 0
Source/AtomicJS/Javascript/JSNetwork.cpp

@@ -0,0 +1,52 @@
+#ifdef ATOMIC_NETWORK
+
+// 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 "JSNetwork.h"
+#include "JSVM.h"
+
+#include <Atomic/Network/HttpRequest.h>
+
+namespace Atomic
+{
+
+static int HttpRequest_Read(duk_context* ctx)
+{
+    duk_push_this(ctx);
+
+    HttpRequest* request = js_to_class_instance<HttpRequest>(ctx, -1, 0);
+
+    if (!request->GetAvailableSize())
+    {
+        duk_push_string(ctx, "");
+        return 1;
+    }
+
+    PODVector<unsigned char> buffer(request->GetAvailableSize());
+
+    request->Read(&buffer[0], buffer.Size());
+
+    duk_push_lstring(ctx, (const char*) &buffer[0], buffer.Size());
+
+    return 1;
+}
+
+void jsapi_init_network(JSVM* vm)
+{
+    duk_context* ctx = vm->GetJSContext();
+
+    js_class_get_prototype(ctx, "Atomic", "HttpRequest");
+    duk_push_c_function(ctx, HttpRequest_Read, 0);
+    duk_put_prop_string(ctx, -2, "read");
+    duk_pop(ctx);
+
+}
+
+}
+
+
+#endif
+
+

+ 10 - 0
Source/AtomicJS/Javascript/JSNetwork.h

@@ -0,0 +1,10 @@
+
+#pragma once
+
+namespace Atomic
+{
+
+class JSVM;
+void jsapi_init_network(JSVM* vm);
+
+}

+ 1 - 1
Source/AtomicJS/Packages/Atomic/Network.json

@@ -2,7 +2,7 @@
 	"name" : "Network",
 	"sources" : ["Source/Atomic/Network"],
 	"includes" : ["<Atomic/Network/Protocol.h>", "<Atomic/Scene/Scene.h>"],
-	"classes" : ["Network", "NetworkPriority"]
+	"classes" : ["Network", "NetworkPriority", "HttpRequest"]
 
 
 }

+ 2 - 0
Source/ToolCore/JSBind/JSBFunction.h

@@ -24,12 +24,14 @@ public:
         isPointer_ = false;
         isReference_ = false;
         isTemplate_ = false;
+        isConst_ = false;
     }
 
     bool isSharedPtr_;
     bool isPointer_;
     bool isReference_;
     bool isTemplate_;
+    bool isConst_;
 
     String name_;
     String initializer_;

+ 31 - 3
Source/ToolCore/JSBind/JSBFunctionWriter.cpp

@@ -178,6 +178,34 @@ void JSBFunctionWriter::WriteParameterMarshal(String& source)
                 }
 
             }
+            else if (ptype->type_->asVectorType())
+            {
+                // read only vector arguments
+                if (ptype->isConst_)
+                {
+                    JSBVectorType* vtype = ptype->type_->asVectorType();
+                    source.AppendWithFormat("%s __arg%i;\n", vtype->ToString().CString(), cparam);
+
+                    source.AppendWithFormat("if (duk_get_top(ctx) >= %i)\n{\n", cparam + 1);
+                    source.AppendWithFormat("duk_require_object_coercible(ctx, %i);\n", cparam);
+                    source.AppendWithFormat("unsigned sz = duk_get_length(ctx, %i);\n", cparam);
+                    source.AppendWithFormat("for (unsigned i = 0; i < sz; i++)\n{\n");
+
+                    source.AppendWithFormat("duk_get_prop_index(ctx, 2, i);\n");
+
+                    if (vtype->vectorType_->asStringType() || vtype->vectorType_->asStringHashType() )
+                    {
+                        source.AppendWithFormat("__arg%i.Push(duk_get_string(ctx, -1));\n", cparam);
+                    }
+
+                    source.AppendWithFormat("duk_pop(ctx);\n");
+
+                    source.AppendWithFormat("\n}\n");
+
+                    source.AppendWithFormat("\n}\n");
+
+                }
+            }
 
         }
     }
@@ -247,9 +275,9 @@ duk_ret_t jsb_constructor_MyJSClass(duk_context* ctx)
      */
 
     source.Append( "\nJSVM* vm = JSVM::GetJSVM(ctx);\n" \
-            "duk_push_this(ctx);\n" \
-            "void *ptr = duk_get_heapptr(ctx, -1);\n" \
-            "duk_pop(ctx);\n\n");
+                   "duk_push_this(ctx);\n" \
+                   "void *ptr = duk_get_heapptr(ctx, -1);\n" \
+                   "duk_pop(ctx);\n\n");
 
     source.Append("   if (!vm->GetObjectPtr(ptr, true))\n   {\n");
 

+ 20 - 2
Source/ToolCore/JSBind/JSBHeaderVisitor.h

@@ -61,7 +61,7 @@ public:
             IntegerType* itype = type->asIntegerType();
             jtype = new JSBPrimitiveType(itype->kind());
         }
-        if (type->isFloatType())
+        else if (type->isFloatType())
         {
             jtype = new JSBPrimitiveType(JSBPrimitiveType::Float);
         }
@@ -73,7 +73,20 @@ public:
             if (classname.StartsWith("Atomic::"))
                 classname.Replace("Atomic::", "");
 
-            if (classname == "String")
+            if (classname == "Vector")
+            {
+                if (ntype->name()->asTemplateNameId())
+                {
+                    const TemplateNameId* tnid = ntype->name()->asTemplateNameId();
+                    FullySpecifiedType pfst = tnid->templateArgumentAt(0);
+                    JSBType* vtype = processTypeConversion(pfst.type());
+                    if (vtype)
+                    {
+                        jtype = new JSBVectorType(vtype);
+                    }
+                }
+            }
+            else if (classname == "String")
             {
                 jtype = new JSBStringType();
             }
@@ -171,6 +184,10 @@ public:
         if (!jtype)
             return NULL;
 
+        // read only vectors atm
+        if (!isConst && jtype->asVectorType())
+            return NULL;
+
         bool skip = false;
 
         // no pointers to prim atm
@@ -192,6 +209,7 @@ public:
         ftype->isSharedPtr_ = isSharedPtr;
         ftype->isReference_ = isReference;
         ftype->isTemplate_ = isTemplate;
+        ftype->isConst_ = isConst;
 
         return ftype;
 

+ 17 - 0
Source/ToolCore/JSBind/JSBType.h

@@ -20,6 +20,7 @@ class JSBStringHashType;
 class JSBClassType;
 class JSBEnumType;
 class JSBHeapPtrType;
+class JSBVectorType;
 
 class JSBType
 {
@@ -32,6 +33,7 @@ public:
     virtual JSBStringHashType* asStringHashType() { return 0; }
     virtual JSBEnumType* asEnumType() { return 0; }
     virtual JSBHeapPtrType* asHeapPtrType() { return 0; }
+    virtual JSBVectorType* asVectorType() { return 0; }
 
     static JSBType* Parse(const String& value);
 
@@ -142,6 +144,21 @@ public:
 
 };
 
+class JSBVectorType : public JSBType
+{
+
+public:
+
+    JSBType* vectorType_;
+
+    JSBVectorType(JSBType* vtype) : vectorType_(vtype) {}
+
+    virtual JSBVectorType* asVectorType() { return this; }
+
+    String ToString() { return "Vector<" + vectorType_->ToString() + ">"; }
+};
+
+
 
 class JSBClassType : public JSBType
 {

+ 5 - 0
Source/ToolCore/JSBind/JSBTypeScript.cpp

@@ -40,6 +40,11 @@ static String GetScriptType(JSBFunctionType* ftype)
     if (ftype->type_->asClassType())
         scriptType = ftype->type_->asClassType()->class_->GetName();
 
+    if (ftype->type_->asVectorType())
+    {
+        scriptType = "string[]";
+    }
+
     return scriptType;
 
 }