Browse Source

JS IO extensions, allow returning a string reference, also allow taking a const string reference parameter

Josh Engebretson 10 years ago
parent
commit
1525e98ebf

+ 17 - 2
Source/AtomicJS/JSBind/JSBHeaderVisitor.h

@@ -113,6 +113,8 @@ public:
         bool isReference = false;
         bool isReference = false;
         bool isTemplate = false;
         bool isTemplate = false;
 
 
+        bool isConst = false;
+
         if (type->isPointerType())
         if (type->isPointerType())
         {
         {
             isPointer=true;
             isPointer=true;
@@ -124,6 +126,7 @@ public:
             isReference=true;
             isReference=true;
             FullySpecifiedType pfst = type->asReferenceType()->elementType();
             FullySpecifiedType pfst = type->asReferenceType()->elementType();
             type = pfst.type();
             type = pfst.type();
+            isConst = pfst.isConst();
         }
         }
         if (!isPointer && retType)
         if (!isPointer && retType)
         {
         {
@@ -160,9 +163,21 @@ public:
         if (!jtype)
         if (!jtype)
             return NULL;
             return NULL;
 
 
+        bool skip = false;
+
         // no pointers to prim atm
         // no pointers to prim atm
-        if ((isPointer || isReference) && jtype->asPrimitiveType())
-            return NULL;
+        if (isPointer || isReference)
+        {
+            if (jtype->asPrimitiveType())
+                skip = true;
+            else if (!retType && !isConst && (jtype->asStringType() || jtype->asStringHashType()))
+            {
+                skip = true;
+            }
+
+            if (skip)
+                return NULL;
+        }
 
 
         JSBFunctionType* ftype = new JSBFunctionType(jtype);
         JSBFunctionType* ftype = new JSBFunctionType(jtype);
         ftype->isPointer_ = isPointer;
         ftype->isPointer_ = isPointer;

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

@@ -15,6 +15,7 @@
 #include "JSVM.h"
 #include "JSVM.h"
 #include "JSComponent.h"
 #include "JSComponent.h"
 #include "JSGraphics.h"
 #include "JSGraphics.h"
+#include "JSIO.h"
 #include "JSUIAPI.h"
 #include "JSUIAPI.h"
 #include "JSScene.h"
 #include "JSScene.h"
 
 
@@ -236,6 +237,7 @@ void jsapi_init_atomic(JSVM* vm)
     jsb_modules_init(vm);
     jsb_modules_init(vm);
 
 
     // extensions
     // extensions
+    jsapi_init_io(vm);
     jsapi_init_graphics(vm);
     jsapi_init_graphics(vm);
     jsapi_init_ui(vm);
     jsapi_init_ui(vm);
     jsapi_init_scene(vm);
     jsapi_init_scene(vm);

+ 35 - 0
Source/AtomicJS/Javascript/JSIO.cpp

@@ -0,0 +1,35 @@
+// 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 "JSIO.h"
+#include "JSVM.h"
+#include <Atomic/IO/File.h>
+
+namespace Atomic
+{
+
+static int File_ReadText(duk_context* ctx)
+{
+    duk_push_this(ctx);
+    File* file = js_to_class_instance<File>(ctx, -1, 0);
+
+    String text;
+    file->ReadText(text);
+
+    duk_push_string(ctx, text.CString());
+
+    return 1;
+}
+
+void jsapi_init_io(JSVM* vm)
+{
+    duk_context* ctx = vm->GetJSContext();
+
+    js_class_get_prototype(ctx, "File");
+    duk_push_c_function(ctx, File_ReadText, 0);
+    duk_put_prop_string(ctx, -2, "readText");
+    duk_pop(ctx);
+}
+
+}

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

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