OSDN Git Service

modified: interpreter.c
authorYoshinobu Date <admin@hackerzlife.com>
Tue, 14 Jun 2011 20:13:40 +0000 (06:13 +1000)
committerYoshinobu Date <admin@hackerzlife.com>
Tue, 14 Jun 2011 20:13:40 +0000 (06:13 +1000)
modified:   interpreter.h
modified:   js.c
modified:   js.h
modified:   main.c
modified:   makefile
modified:   test.js
modified:   xaw.c
modified:   xaw.h

interpreter.c
interpreter.h
js.c
js.h
main.c
makefile
test.js
xaw.c
xaw.h

index 19aa030..ca575d7 100644 (file)
@@ -1,13 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include "interpreter.h"
 
-#define ARG_ERR_MSG "Wrong argument. Aborting…\n"
 
-#include <JavascriptCore/JavascriptCore.h>//-framework JavaScriptCoreオプションを使用の事
-
-extern void functionLoader(JSGlobalContextRef ctx, JSObjectRef jobjGlobal);
-void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject);
-void functionWrapper(JSGlobalContextRef ctx, JSObjectRef parentObj, char* name, JSObjectCallAsFunctionCallback jsGlobalFunction);
 
 int interpreter(char* code)
 {
@@ -25,22 +20,15 @@ int interpreter(char* code)
     printf("Done!\n");
 #endif
        
-    // グローバル実行コンテキストが持つグローバルオブジェクトを取得する
-    // ブラウザでいうところの window オブジェクトのようなもの
     JSObjectRef jobjGlobal = JSContextGetGlobalObject(ctx);
-       
     functionLoader(ctx, jobjGlobal);
-       
-    // JavaScript のソースを JS の文字列にする
     JSStringRef jstrSource = JSStringCreateWithUTF8CString(code);
        
 #ifdef DEBUG
     printf("JavaScript source is now ready to execute. Starting ..\n\n");
 #endif
        
-    // 実行、 this を NULL とするとグローバルオブジェクトが this になるらしい
     JSEvaluateScript(ctx, jstrSource, NULL, NULL, 1, NULL); 
-    // 文字列のリファレンスカウンタをデクリメント
     JSStringRelease(jstrSource);
        
 #ifdef DEBUG
@@ -55,7 +43,7 @@ int interpreter(char* code)
        
 }
 
-void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject)
+void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject, JSObjectCallAsConstructorCallback callAsConstructor)
 {
        //クラスの定義を保持する変数を作成
        JSClassDefinition classDefinition       = kJSClassDefinitionEmpty;
@@ -68,7 +56,7 @@ void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JS
        //クラス型を作成
        classSubstance = JSClassCreate(&classDefinition);
        //クラスをオブジェクト化
-       *classObject = JSObjectMakeConstructor(ctx, classSubstance, NULL);
+       *classObject = JSObjectMakeConstructor(ctx, classSubstance, callAsConstructor);
        //プロトタイプ継承
        JSObjectSetPrototype(ctx, *classObject, JSObjectGetPrototype(ctx, jobjGlobal));
        //クラスを持ったオブジェクトの名前を作る
index 755b23c..207977e 100644 (file)
@@ -7,12 +7,12 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-
-
+       
+       
        extern void functionLoader(JSGlobalContextRef ctx, JSObjectRef jobjGlobal);
        void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject, JSObjectCallAsConstructorCallback callAsConstructor);
        void functionWrapper(JSGlobalContextRef ctx, JSObjectRef parentObj, char* name, JSObjectCallAsFunctionCallback jsGlobalFunction);
-
+       
 #ifdef __cplusplus
 }
 #endif
diff --git a/js.c b/js.c
index c48f3e4..ba5f7a0 100644 (file)
--- a/js.c
+++ b/js.c
@@ -12,8 +12,8 @@ extern void functionLoader_Gui(JSGlobalContextRef ctx, JSObjectRef jobjGlobal);
 void functionLoader(JSGlobalContextRef ctx, JSObjectRef jobjGlobal)
 {      
        JSObjectRef Console;
-       classWrapper(ctx, jobjGlobal, "Console", &Console);
-//     functionWrapper(ctx, Console, "Write", (JSObjectCallAsFunctionCallback)jsGlobalPrint);
+       classWrapper(ctx, jobjGlobal, "Console", &Console, NULL);
+       functionWrapper(ctx, Console, "Write", (JSObjectCallAsFunctionCallback)jsGlobalPrint);
        functionWrapper(ctx, Console, "Read", (JSObjectCallAsFunctionCallback)jsGlobalScan);
 #ifdef ENABLE_GUI
     functionLoader_Gui(ctx, jobjGlobal);//(xaw.cを参照のこと。)
@@ -24,21 +24,21 @@ void functionLoader(JSGlobalContextRef ctx, JSObjectRef jobjGlobal)
        functionWrapper(ctx, jobjGlobal, "call", (JSObjectCallAsFunctionCallback)jsGlobalCaller);
        functionWrapper(ctx, jobjGlobal, "exit", (JSObjectCallAsFunctionCallback)jsGlobalExit);
        functionWrapper(ctx, jobjGlobal, "test", (JSObjectCallAsFunctionCallback)jsGlobalTest);
-
+       
 }
 
 //-------------------
 
 
 static JSValueRef jsGlobalCaller(
-                        JSContextRef        ctx,
-                        JSObjectRef         jobj,
-                        JSObjectRef         jobjThis,
-                        size_t              argLen,
-                        const JSObjectRef   args[],
-                        JSValueRef*         jobjExp) {
-
-
+                                                                JSContextRef        ctx,
+                                                                JSObjectRef         jobj,
+                                                                JSObjectRef         jobjThis,
+                                                                size_t              argLen,
+                                                                const JSObjectRef   args[],
+                                                                JSValueRef*         jobjExp) {
+       
+       
        if (argLen) {
         // 第一引数を JS の文字列としてコピー
         JSStringRef     jstrArg = JSValueToStringCopy(ctx, args[0], jobjExp);
@@ -48,7 +48,7 @@ static JSValueRef jsGlobalCaller(
         char*           szArg   =  (char*)malloc(len);
         
         JSStringGetUTF8CString(jstrArg, szArg, len);
-
+               
                callJsFunction(ctx, szArg);
                
         JSStringRelease(jstrArg);
@@ -154,13 +154,13 @@ static JSValueRef jsGlobalScan(
 }
 
 static JSValueRef jsGlobalF_Open(
-                                                               JSContextRef        ctx,
-                                                               JSObjectRef         jobj,
-                                                               JSObjectRef         jobjThis,
-                                                               size_t              argLen,
-                                                               const JSObjectRef   args[],
-                                                               JSValueRef*         jobjExp) {
-
+                                                                JSContextRef        ctx,
+                                                                JSObjectRef         jobj,
+                                                                JSObjectRef         jobjThis,
+                                                                size_t              argLen,
+                                                                const JSObjectRef   args[],
+                                                                JSValueRef*         jobjExp) {
+       
     if (argLen) {
         // 第一引数を JS の文字列としてコピー
         JSStringRef     jstrArg = JSValueToStringCopy(ctx, args[0], jobjExp);
@@ -177,21 +177,21 @@ static JSValueRef jsGlobalF_Open(
         free(szArg);
         JSStringRelease(jstrArg);
         JSStringRelease(mystring);
-
+               
         return result;
     }
-
+       
     // undefined を作って返す
     return JSValueMakeUndefined(ctx);
 }
 
 static JSValueRef jsGlobalExec(
-                                                                JSContextRef        ctx,
-                                                                JSObjectRef         jobj,
-                                                                JSObjectRef         jobjThis,
-                                                                size_t              argLen,
-                                                                const JSObjectRef   args[],
-                                                                JSValueRef*         jobjExp) {
+                                                          JSContextRef        ctx,
+                                                          JSObjectRef         jobj,
+                                                          JSObjectRef         jobjThis,
+                                                          size_t              argLen,
+                                                          const JSObjectRef   args[],
+                                                          JSValueRef*         jobjExp) {
        
     if (argLen) {
         // 第一引数を JS の文字列としてコピー
@@ -251,14 +251,14 @@ static JSValueRef jsGlobalPrint(
 static JSValueRef makeException(JSGlobalContextRef ctx, char* ename)
 {
        if (ename) {
-       JSStringRef jstrEx = JSStringCreateWithUTF8CString(ename);
-       
-       JSValueRef argumentsErrorValues[] = { JSValueMakeString(ctx, jstrEx) };
-       
-       JSObjectRef e = JSObjectMakeError(ctx, 1, argumentsErrorValues, NULL);
-       printf("Error: %s",ename);
-       
-       JSStringRelease(jstrEx);
+               JSStringRef jstrEx = JSStringCreateWithUTF8CString(ename);
+               
+               JSValueRef argumentsErrorValues[] = { JSValueMakeString(ctx, jstrEx) };
+               
+               JSObjectRef e = JSObjectMakeError(ctx, 1, argumentsErrorValues, NULL);
+               printf("Error: %s",ename);
+               
+               JSStringRelease(jstrEx);
                return e;
        }
        
@@ -266,12 +266,12 @@ static JSValueRef makeException(JSGlobalContextRef ctx, char* ename)
 }
 
 static JSValueRef jsGlobalExit(
-                                                                JSContextRef        ctx,
-                                                                JSObjectRef         jobj,
-                                                                JSObjectRef         jobjThis,
-                                                                size_t              argLen,
-                                                                const JSObjectRef   args[],
-                                                                JSValueRef*         jobjExp) {
+                                                          JSContextRef        ctx,
+                                                          JSObjectRef         jobj,
+                                                          JSObjectRef         jobjThis,
+                                                          size_t              argLen,
+                                                          const JSObjectRef   args[],
+                                                          JSValueRef*         jobjExp) {
        
        exit(0);
     // undefined を作って返す
diff --git a/js.h b/js.h
index 8646878..2d06dfd 100644 (file)
--- a/js.h
+++ b/js.h
@@ -51,16 +51,16 @@ static JSValueRef jsGlobalCaller(
                                                                 const JSObjectRef   args[],
                                                                 JSValueRef*         jobjExp);
 static JSValueRef jsGlobalExit(
-                                                                JSContextRef        ctx,
-                                                                JSObjectRef         jobj,
-                                                                JSObjectRef         jobjThis,
-                                                                size_t              argLen,
-                                                                const JSObjectRef   args[],
-                                                                JSValueRef*         jobjExp);
+                                                          JSContextRef        ctx,
+                                                          JSObjectRef         jobj,
+                                                          JSObjectRef         jobjThis,
+                                                          size_t              argLen,
+                                                          const JSObjectRef   args[],
+                                                          JSValueRef*         jobjExp);
 
 extern int interpreter(char* code);
 extern void functionWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectCallAsFunctionCallback jsGlobalFunction);
-extern void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject);
+extern void classWrapper(JSGlobalContextRef ctx, JSObjectRef jobjGlobal, char* name, JSObjectRef* classObject, JSObjectCallAsConstructorCallback callAsConstructor);
 extern int dialogtest( int argc, char **argv );
 extern int g_argc;
 extern char** g_argv;
diff --git a/main.c b/main.c
index c375abf..5d5a108 100644 (file)
--- a/main.c
+++ b/main.c
@@ -13,10 +13,10 @@ int main(int argc, char** argv)
 {
        g_argc = argc;
        g_argv = argv;
-    #ifdef DEBUG
+#ifdef DEBUG
     printf("Program loaded.\n");
-    #endif
-
+#endif
+       
     switch(argc)
     {
         case 1:
@@ -28,7 +28,7 @@ int main(int argc, char** argv)
         case 3:
             if (!strcmp( argv[1], "-x" )){return interpreter(*(argv+2)); }
             else{
-puts(ARG_ERR_MSG); return -1;}
+                               puts(ARG_ERR_MSG); return -1;}
             break;
         default:
             puts(ARG_ERR_MSG);
@@ -36,6 +36,6 @@ puts(ARG_ERR_MSG); return -1;}
     }
        
        return 0;
-
+       
 }
 
index 61fc7e5..b320f93 100644 (file)
--- a/makefile
+++ b/makefile
@@ -1,5 +1,6 @@
 # Makefile
 CC = gcc
+CCP = g++
 TARGET = js_gui
 OBJECT =  \
  main.c \
@@ -26,7 +27,7 @@ FRAEMEWORK = -framework JavaScriptCore
 
 $(TARGET) : $(OBJECT)
        @echo "*** Making $(TARGET) ***"
-       $(CC) -O3 $(FRAEMEWORK) $(LIB) -DENABLE_GUI $(DEF) -g -o $(TARGET) $(OBJECT)
+       $(CC) -O0 $(FRAEMEWORK) $(LIB) -DENABLE_GUI $(DEF) -g -o $(TARGET) $(OBJECT)
 
 
 js : main.c js.c interpreter.c
diff --git a/test.js b/test.js
index 413082d..6ea0347 100644 (file)
--- a/test.js
+++ b/test.js
@@ -1,63 +1,25 @@
+var d = new Date();
+
 var quit = function(){
-exit();
+       exit();
 };
 var x = 1;
 var count = function (){
-    print(x);
+       d = new Date();
+    label.Text = "count="+x+"\n"+d;
        x++;
+       setTimeout( "count()", 1000 );
+};
+var chtxt = function (){
+       label.Text = "Changed";
 };
 
-var d = new Date();
-
-var str = Console.Read();
-print(str);
-
-var mywindow = new xawWindow;
-
-mywindow.Name = "form";
-mywindow.Direction = 0;
-mywindow.SetProperty();
-
-
-var label = new xawControl;
-
-label.Type=0;
-label.Parent=mywindow;
-label.Text=d.toString();
-label.Width=280;
-label.Height=20;
-label.Left=0;
-label.Top=0;
-label.Border=1;
-label.Create();
-
-/*var button = new xawControl;
-
-button.Type=1;
-button.Parent=mywindow;
-button.Text="Exit";
-button.Width=105;
-button.Height=20;
-button.Left=150;
-button.Top=20;
-button.Border=1;
-button.Callback = quit;
-
-button.Create();
-
-var button2 = new xawControl;
-
-button2.Type=1;
-button2.Parent=mywindow;
-button2.Text="Count";
-button2.Width=145;
-button2.Height=20;
-button2.Left=0;
-button2.Top=20;
-button2.Border=1;
-button2.Callback = count;
 
-button2.Create();
+var mywindow = new xawWindow("Test");
+var form = new xawForm(mywindow, "form", 0);
+var label = new xawWidget(form, "Hello world!\n"+d);
+print("objects ready.\n");
+count();
+mywindow.Show();
 
 
-mywindow.Show();
\ No newline at end of file
diff --git a/xaw.c b/xaw.c
index ada6a52..2787f51 100644 (file)
--- a/xaw.c
+++ b/xaw.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include "xaw.h"
 
 //***  Strong suggetion for mac users to overwrite Intrinsic.h  ***
@@ -7,12 +8,11 @@
 
 void functionLoader_Gui(JSGlobalContextRef ctx, JSObjectRef jobjGlobal);
 XtAppContext app_con;
-XtOrientation xawOrientationDirection[]={XtorientHorizontal, XtorientVertical};
-
-
-volatile Widget *myWidgets;
-volatile xawWidet *myCtrls;
+JSContextRef jsGlobalCtx;
+JSObjectRef tmp;
+const XtOrientation xawOrientationDirection[]={XtorientHorizontal, XtorientVertical};
 
+Widget *myWidgets;
 int objIDc = 0;
 
 volatile jsCallbackInfo *myCallBacks;
@@ -36,247 +36,386 @@ void backgroundColorChanged(Widget w,char*colorStr){
 #pragma GCC optimization_level 0
 void xawJsCallbackFunction( Widget w, XtPointer client, XtPointer called )
 {
-       JSObjectCallAsFunction((*(jsCallbackInfo*)client).Context, (*(jsCallbackInfo*)client).Function, NULL, 0,resources,NULL);
+       //JSObjectCallAsFunction((*(jsCallbackInfo*)client).Context, (*(jsCallbackInfo*)client).Function, NULL, 0,resources,NULL);
 }
 #pragma GCC optimization_level reset
 
-Widget createForm(Widget* form, xawWindow window)
+static JSValueRef jsXawWindowGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
+{
+       JSValueRef result = NULL;
+       //printf("Get:");
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(propertyName);
+       char* pname   =  (char*)malloc(len);
+       
+       JSStringGetUTF8CString(propertyName, pname, len);
+       //printf("%s\n",pname);
+       
+       if (!strcmp(pname, "test")) {
+               result = JSValueMakeNumber(ctx, 1.23);
+       }
+       
+       free(pname);
+       return result;
+}
+static bool jsXawWindowSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
 {
-       Widget top;
+       printf("Set\n");
+       bool setSuccess = FALSE;
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
+       char*           propertyName   =  (char*)malloc(len);
+       JSStringGetUTF8CString(jsPropertyName, propertyName, len);
+       
+       
+       if(!strcmp( propertyName, "direction" ))
+       {
+               setSuccess = TRUE;
+       }
+       
+       
+       
+       free(propertyName);
+       return setSuccess;
+}
 
-       /* ツールキットの初期化 */
-       top = XtVaAppInitialize( window.Context, window.Name, NULL, 0, &(window.argc), window.argv, NULL, NULL );
-       /*  クラス名       引数のリソース指定を解釈*/
-       /* 台紙ウィジットの作成 */            /* ウィジットクラス */
-       *form = XtVaCreateManagedWidget( "form", formWidgetClass, top, 
-                                   /* ウィジット名 */      /* 親ウィンドウ */
-                                                                  /* リソースの指定 */                XtNorientation, window.Direction,
-                                                                  /* いくつでもOKなので最後はNULL */  NULL );
-       return top;
+static JSValueRef jsXawWindowShow(
+                                                                 JSContextRef        ctx,
+                                                                 JSObjectRef         jobj,
+                                                                 JSObjectRef         jobjThis,
+                                                                 size_t              argLen,
+                                                                 const JSObjectRef   args[],
+                                                                 JSValueRef*         jobjExp)
+{
+       jsGlobalCtx = ctx;
+       
+       const int parentObjectID = (int)JSObjectGetPrivate(jobjThis); 
+       
+       XtRealizeWidget( myWidgets[parentObjectID] );
+       XtAppMainLoop( app_con );
+       
+    // undefined を作って返す
+    return JSValueMakeUndefined(ctx);
 }
-#pragma GCC optimization_level 0
-Widget createControl(xawWidet widget, jsCallbackInfo* callinfo)
+
+static JSObjectRef jsXawWindowConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
-       myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+2) );
+       myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
        if( myWidgets == NULL )//ポインタが空っぽだったら実行停止。
        {
-               printf( "Memory re-allocation failed!\n" );
+               printf( "Memory alloc failed!\n" );
                exit( 1 );
        }
        
-       myWidgets[objIDc+2] =0;
-       myWidgets[objIDc+2] = XtVaCreateManagedWidget( widget.Name, widget.Type, widget.Parent,
-                                                                  XtNlabel, widget.Text,
-                                                                  XtNwidth, widget.Width, XtNheight, widget.Height,
-                                                                       /* 縁取り */             XtNborderWidth, widget.Border,
-                                                                  /* 配置方法 */           XtNfromHoriz, NULL,
-                                                                  /* ウィンドウの左端から */  XtNhorizDistance, widget.Left,
-                                                                  /* ウィンドウの上端から */  XtNfromVert, NULL,
-                                                                                                                  XtNvertDistance, widget.Top,
-                                                                  NULL );
        
-       Colormap cmap;
-       XColor c0,c1;
-       cmap = DefaultColormap(XtDisplay(myWidgets[objIDc+2]),0);
-       XAllocNamedColor(XtDisplay(myWidgets[objIDc+2]),cmap,"rgb:d4/d4/d4",&c1,&c0);
-       XtVaSetValues(myWidgets[objIDc+2], XtNbackground,c1.pixel, NULL );
+       /********************************************************************************\
+        Create a whole new class definition for to create new window object with get-set. 
+        \********************************************************************************/
+       
+       JSClassDefinition classDefinition       = kJSClassDefinitionEmpty;
+       classDefinition.className = "Window";
+       classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
+       classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawWindowGetProperty;
+       classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawWindowSetProperty;
+       
+       /********************************************************************************\
+        Call XAW to put xwindow instance up.
+        var mywindow = new xawWindow("form", 0);
+        \********************************************************************************/
+       
+       char* windowName = "Untitled Window";
+       double windowDirection = 0;
+       
+       if (argumentCount) {
+        JSStringRef     jstrArg = JSValueToStringCopy(ctx, arguments[0], exception);
+        size_t          len     = JSStringGetMaximumUTF8CStringSize(jstrArg);
+        windowName   =  (char*)malloc(len);
+               
+        JSStringGetUTF8CString(jstrArg, windowName, len);
+        JSStringRelease(jstrArg);
+    }
+       
+       myWidgets[objIDc] = XtVaAppInitialize( &app_con, windowName, NULL, 0, &g_argc, g_argv, NULL, NULL);
+       
+       //Construct a class and create object out of it
+       JSObjectRef windowObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
+       functionWrapper(ctx, constructor, "Show", (JSObjectCallAsFunctionCallback)jsXawWindowShow);
+       JSObjectSetPrivate(windowObj, (void*)objIDc);
+       
        
-       XtAddCallback( myWidgets[objIDc+2], XtNcallback, xawJsCallbackFunction, callinfo );
        objIDc++;
-       return myWidgets[objIDc+2];
+       return windowObj;  
+       
 }
 
-
-static JSValueRef jsControlCreate(
-                                                                JSContextRef        ctx,
-                                                                JSObjectRef         jobj,
-                                                                JSObjectRef         jobjThis,
-                                                                size_t              argLen,
-                                                                const JSObjectRef   args[],
-                                                                JSValueRef*         jobjExp)
+static JSValueRef jsXawFormGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
 {
-       JSValueRef r = JSValueMakeNumber(ctx,  -1);
-
-       //type
-       int typenum     = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Type"), jobjExp),NULL);   
-       WidgetClass type;
-       switch(typenum)
+       printf("Get:");
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(propertyName);
+       char* pname   =  (char*)malloc(len);
+       
+       JSStringGetUTF8CString(propertyName, pname, len);
+       printf("%s\n",pname);
+       free(pname);
+       return JSValueMakeNumber(ctx, 1.23);
+}
+static bool jsXawFormSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
+{
+       printf("Set\n");
+       bool setSuccess = FALSE;
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
+       char*           propertyName   =  (char*)malloc(len);
+       JSStringGetUTF8CString(jsPropertyName, propertyName, len);
+       
+       
+       if(!strcmp( propertyName, "direction" ))
        {
-                       case 0:
-                               type = labelWidgetClass;
-                               break;
-                       case 1:
-                               type = commandWidgetClass;
-                               break;
-                       case 2:
-                       type = asciiTextWidgetClass;
-                       break;
+               setSuccess = TRUE;
        }
-       //*********
-       
-       //parent ///???
-       int parentID    = (int)JSValueToNumber(ctx, 
-                                                                                  JSObjectGetProperty(ctx,
-                                                                                                                          (JSObjectRef)JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Parent"), jobjExp),
-                                                                                                                       JSStringCreateWithUTF8CString("ID"),
-                                                                                                                       jobjExp),NULL); 
-       //-------
-       parentID++;
-       
-       //text
-       JSStringRef     jstrText = JSValueToStringCopy(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Text"), jobjExp), jobjExp);
-       size_t len     = JSStringGetMaximumUTF8CStringSize(jstrText);
-       char*           text   =  (char*)malloc(len);
-       JSStringGetUTF8CString(jstrText, text, len);
-       //**********
-       
-       int W   = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Width"), jobjExp),NULL);  
-       int H   = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Height"), jobjExp),NULL); 
-       int L   = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Left"), jobjExp),NULL);   
-       int T   = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Top"), jobjExp),NULL);
-       int B   = (int)JSValueToNumber(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Border"), jobjExp),NULL);
        
-
-       JSStringRef     jstrCallback = JSValueToStringCopy(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Callback"), jobjExp), jobjExp);
-       len     = JSStringGetMaximumUTF8CStringSize(jstrCallback);
-       char*           func   =  (char*)malloc(len);
-       JSStringGetUTF8CString(jstrCallback, func, len);
-
-       myCtrls = (xawWidet *)realloc( (void*)myCtrls, sizeof(xawWidet)*(objIDc+2) );
-       if( myCtrls == NULL )//ポインタが空っぽだったら実行停止。
+       
+       free(propertyName);
+       return setSuccess;
+}
+static JSObjectRef jsXawFormConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+       const int parentObjectID = (int)JSObjectGetPrivate(JSValueToObject(ctx,arguments[0],exception)); 
+       //myWidgets[parentObjectID] is the way of access of parental Widget of the Widget this function is about to create 
+       //printf("recieved:%d\n", parentObjectID);
+       //printf("FormConstructor: arg count = %d\n", argumentCount);
+       
+       //Add heap area to accomodate a new Widget
+       myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
+       if( myWidgets == NULL )//stop if empty pointer for some reason
        {
-               printf( "Memory re-allocation failed!\n" );
-               exit( 1 );
+               printf( "Memory alloc failed!\n" );
+               return JSValueToObject(ctx, JSValueMakeNull(ctx), exception); 
        }
-       myCallBacks = (jsCallbackInfo *)realloc( (void*)myCallBacks, sizeof(xawWidet)*(objIDc+2) );
-       if( myCallBacks == NULL )//ポインタが空っぽだったら実行停止。
-       {
-               printf( "Memory re-allocation failed!\n" );
-               exit( 1 );
+       
+       /********************************************************************************\
+        Create a whole new class definition for to create new window object with get-set. 
+        \********************************************************************************/
+       
+       JSClassDefinition classDefinition       = kJSClassDefinitionEmpty;
+       classDefinition.className = "Form";
+       classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
+       classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawFormGetProperty;
+       classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawFormSetProperty;
+       
+       
+       /********************************************************************************\
+        Call XAW to put xwindow instance up.
+        var form = new xawForm(mywindow, "form", 0);
+        \********************************************************************************/
+       
+       
+       char* formName = "Untitled form";
+       double formDirection = 0;
+       
+       
+       if (argumentCount >= 2) {
+        JSStringRef     jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
+        size_t          len     = JSStringGetMaximumUTF8CStringSize(jstrArg);
+        formName   =  (char*)malloc(len);
+               
+        JSStringGetUTF8CString(jstrArg, formName, len);
+        JSStringRelease(jstrArg);
+    }
+       
+       XtOrientation o = XtorientVertical;
+       if (argumentCount == 3) {
+               formDirection = JSValueToNumber(ctx, arguments[2], exception);
+               o = (formDirection==0)?XtorientVertical:XtorientHorizontal;
        }
        
-       char*           name   =  (char*)malloc(sizeof(char)*16);
-       sprintf(name, "%d", objIDc+2);
-       myCtrls[objIDc+2].Name = name;
-       //myCtrl.Name = name;
-       myCtrls[objIDc+2].Type = type;
-       myCtrls[objIDc+2].Parent = myWidgets[parentID];
-       myCtrls[objIDc+2].Text = text;
-       myCtrls[objIDc+2].Width = W;//x
-       myCtrls[objIDc+2].Height = H;//y
-       myCtrls[objIDc+2].Left = L;//x
-       myCtrls[objIDc+2].Top = T;//y
-       myCtrls[objIDc+2].Border = B;
+       myWidgets[objIDc] = XtVaCreateManagedWidget(formName, formWidgetClass, myWidgets[parentObjectID], XtNorientation, o, NULL);
        
-       //jsCallbackInfo mycallback;
-       myCallBacks[objIDc+2].Context = ctx;
+       //Construct a class and create object out of it, send it back to JavaScript.
+       JSObjectRef formObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
+       JSObjectSetPrivate(formObj, (void*)objIDc);
        
        
-       if(!JSValueIsUndefined(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Callback"), NULL)))
+       objIDc++;
+       return formObj;
+}
+
+static JSValueRef jsXawLabelGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
+{
+       printf("Get:");
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(propertyName);
+       char* pname   =  (char*)malloc(len);
+       
+       JSStringGetUTF8CString(propertyName, pname, len);
+       printf("%s\n",pname);
+       free(pname);
+       return JSValueMakeNumber(ctx, 1.23);
+}
+static bool jsXawLabelSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
+{
+       int objectID = (int)JSObjectGetPrivate(object);
+       //printf("jsXawWidget[%d].Set.", objectID );
+       bool setSuccess = FALSE;
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
+       char*           propertyName   =  (char*)malloc(len);
+       JSStringGetUTF8CString(jsPropertyName, propertyName, len);
+       
+       JSStringRef strOfSetValueTo = JSValueToStringCopy(ctx, value, *exception);
+       len     = JSStringGetMaximumUTF8CStringSize(strOfSetValueTo);
+       char*           strOfSetTo   =  (char*)malloc(len);
+       JSStringGetUTF8CString(strOfSetValueTo, strOfSetTo, len);
+       //printf("%s = %s\n",propertyName,strOfSetTo);
+       
+       if(!strcmp( propertyName, "direction" ))
        {
-               myCallBacks[objIDc+2].Function = JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Callback"), NULL);
-       }else {
-               myCallBacks[objIDc+2].Function = NULL;
+               setSuccess = TRUE;
+       }else if(!strcmp( propertyName, "Text" ))
+       {
+               Arg args[1];
+               XtSetArg(args[0],XtNlabel,(XtArgVal)strOfSetTo);
+               XtSetValues(myWidgets[objectID],args,1);
+               //XClearArea(XtDisplay(myWidgets[objectID]), XtWindow(myWidgets[objectID]), 0, 0, 0, 0, True);//NG. BadWindow
+               setSuccess = TRUE;
        }
-
-
        
-       Widget  w = createControl(myCtrls[objIDc+2], &(myCallBacks[objIDc+2]));
-       r = JSValueMakeNumber(ctx,  objIDc);
-       JSObjectSetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("ID"), r, kJSPropertyAttributeNone, NULL);
-       return r;
+       
+       free(propertyName);
+       return setSuccess;
 }
-#pragma GCC optimization_level reset
-
-static JSValueRef jsWindowCreate(
-                                JSContextRef        ctx,
-                                JSObjectRef         jobj,
-                                JSObjectRef         jobjThis,
-                                size_t              argLen,
-                                const JSObjectRef   args[],
-                                JSValueRef*         jobjExp)
+static JSObjectRef jsXawWidgetConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
+       const int parentObjectID = (int)JSObjectGetPrivate(JSValueToObject(ctx,arguments[0],exception)); 
+       //myWidgets[parentObjectID] is the way of access of parental Widget of the Widget this function is about to create 
+       //printf("recieved:%d\n", parentObjectID);
+       //printf("WidgetConstructor: arg count = %d\n", argumentCount);
        
-       //XtSetLanguageProc (app_con, NULL, NULL);
-
-       myWidgets = (Widget *)malloc( sizeof(Widget)*2 );
-       if( myWidgets == NULL )//ポインタが空っぽだったら実行停止。
+       //Add heap area to accomodate a new Widget
+       myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
+       if( myWidgets == NULL )//stop if empty pointer for some reason
        {
                printf( "Memory alloc failed!\n" );
-               exit( 1 );
+               return JSValueToObject(ctx, JSValueMakeNull(ctx), exception); 
        }
-       xawWindow myWindow;
        
-       JSValueRef r = JSValueMakeNumber(ctx,  -1);
-       //if(JSObjectIsConstructor(ctx, jobj))
-       
-        JSStringRef     jstrName = JSValueToStringCopy(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Name"), jobjExp), jobjExp);
-        size_t          len     = JSStringGetMaximumUTF8CStringSize(jstrName);
-        char*           name   =  (char*)malloc(len);
-        JSStringGetUTF8CString(jstrName, name, len);
+       if (argumentCount >= 2)
+       {
+               char widgetName[8]={'W','i','d','g','e','t','_',(char)objIDc+48};
+               printf("Untitled Widget%d on %d", objIDc, parentObjectID);
+               double widgetDirection = 0;
                
-               myWindow.Context = &app_con;
-               myWindow.argc = g_argc;
-               myWindow.argv = g_argv;
-               myWindow.Name = name;
-               myWindow.Direction = (JSValueToBoolean(ctx, JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("Name"), jobjExp)))?XtorientVertical:XtorientHorizontal;
+               /********************************************************************************\
+                Create a whole new class definition for to create new window object with get-set. 
+                \********************************************************************************/
+               
+               JSClassDefinition classDefinition       = kJSClassDefinitionEmpty;
+               classDefinition.className = "Widget";
+               classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
+               JSObjectRef widgetObj = NULL;
+               
+               /********************************************************************************\
+                Determine what kind of widget is required to be made 
+                \********************************************************************************/
+               
+               if((argumentCount == 2) && JSValueIsString(ctx, arguments[1]))
+               {//Label
+                       printf("Label selected for widget %d on %d.\n", objIDc,parentObjectID);
+                       
+                       
+                       classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawLabelGetProperty;
+                       classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawLabelSetProperty;
+                       
+                       /********************************************************************************\
+                        Call XAW to put xwindow instance up.
+                        var Widget = new xawWidget(mywindow, "Widget", 0);
+                        \********************************************************************************/
+                       
+                       
+                       JSStringRef     jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
+                       size_t          len     = JSStringGetMaximumUTF8CStringSize(jstrArg);
+                       char* text   =  (char*)malloc(len);
+                       
+                       JSStringGetUTF8CString(jstrArg, text, len);
+                       JSStringRelease(jstrArg);
+                       
+                       
+                       myWidgets[objIDc] = XtVaCreateManagedWidget(widgetName, labelWidgetClass, myWidgets[parentObjectID], XtNlabel, text, NULL);
+                       
+                       //Construct a class and create object out of it, send it back to JavaScript.
+                       widgetObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
+                       JSObjectSetPrivate(widgetObj, (void*)objIDc);
+                       
+               }else {
+                       printf("Nothing selected for widget %d on %d.\n", objIDc,parentObjectID);
+               }
                
-               myWidgets[objIDc] = createForm(&myWidgets[objIDc + 1], myWindow);
-
-
                
                
-               r = JSValueMakeNumber(ctx, objIDc);
-               JSObjectSetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("ID"), r, kJSPropertyAttributeNone, NULL);
+               
+               //XtAddCallback( myWidgets[objIDc], XtNcallback, xawJsCallbackFunction, callinfo );
+               
+               objIDc++;
+               return widgetObj;
+       }
+       return NULL;
+}
+#pragma GCC optimization_level 0
+void jsXawAppAddTimeOutCallback(XtPointer client)
+{
+       jsCallbackInfo* timeOutCallback = (jsCallbackInfo*)client;
+       assert(((*timeOutCallback).Context != NULL) && ((*timeOutCallback).Function != NULL) );
+       //printf("timeout.\n");
+       JSContextRef ctx = jsGlobalCtx;//timeOutCallback->Context does not work.
+       const JSObjectRef f = timeOutCallback->Function;
        
-       objIDc ++;
+       if (JSValueIsString(ctx, f)) {
+               JSStringRef     jstrSource = JSValueToStringCopy(ctx, f, NULL);
+               JSEvaluateScript(ctx, jstrSource, NULL, NULL, 1, NULL); 
+               JSStringRelease(jstrSource);
+       }else {
+               JSObjectCallAsFunction(jsGlobalCtx, timeOutCallback->Function , NULL, 0,NULL,NULL); 
+       }
+       
+       free(timeOutCallback);
+       timeOutCallback = NULL;
        
-       return r;
 }
+#pragma GCC optimization_level reset
 
-static JSValueRef jsWindowShow(
-                                                 JSContextRef        ctx,
-                                                 JSObjectRef         jobj,
-                                                 JSObjectRef         jobjThis,
-                                                 size_t              argLen,
-                                                 const JSObjectRef   args[],
-                                                 JSValueRef*         jobjExp)
+static JSValueRef jsXawAppAddTimeOut(
+                                                                        JSContextRef        ctx,
+                                                                        JSObjectRef         jobj,
+                                                                        JSObjectRef         jobjThis,
+                                                                        size_t              argLen,
+                                                                        const JSObjectRef   args[],
+                                                                        JSValueRef*         jobjExp)
 {
-       //jobjThis.ID
-       JSValueRef objID = JSObjectGetProperty(ctx, jobjThis, JSStringCreateWithUTF8CString("ID"), jobjExp);
        
-       int windowID = (int)JSValueToNumber(ctx,objID,jobjExp);
-       XtRealizeWidget( myWidgets[windowID] );
-       XtAppMainLoop( app_con ); 
+       //Add heap area to accomodate a new Widget
+       jsCallbackInfo* timeOutCallback = (jsCallbackInfo *)malloc( sizeof(jsCallbackInfo));
+       if( myWidgets == NULL )//stop if empty pointer for some reason
+       {
+               printf( "Memory alloc failed!\n" );
+               return JSValueToObject(ctx, JSValueMakeNull(ctx), jobjExp); 
+       }
+       
+       
+       if ( argLen == 2 && JSValueIsNumber(ctx,args[1]) ) {
+               memmove(timeOutCallback, &((jsCallbackInfo){ctx, args[0]}), sizeof(jsCallbackInfo));
+               
+               JSObjectCallAsFunction(ctx, args[0], NULL, 0,NULL,NULL); 
+               tmp = args[0];
+               XtAppAddTimeOut(app_con, (unsigned long)JSValueToNumber( ctx, args[1], jobjExp), (XtTimerCallbackProc)jsXawAppAddTimeOutCallback,(XtPointer)timeOutCallback);
+    }
+       
+    return JSValueMakeUndefined(ctx);
 }
 
 
 void functionLoader_Gui(JSGlobalContextRef ctx, JSObjectRef jobjGlobal)
 {
-       JSObjectRef Window, Widget;
-       printf("GUI has been enabled with this version.\nEnjoy!\n");
-       classWrapper(ctx, jobjGlobal, "xawWindow", &Window);
-       
-       JSObjectRef Window_Name, Window_Direction;
-       Window_Name = JSValueMakeString(ctx, JSStringCreateWithUTF8CString("undefined"));
-       JSObjectSetProperty(ctx, Window, JSStringCreateWithUTF8CString("Name"), Window_Name, kJSPropertyAttributeNone, NULL);   
-       JSObjectSetProperty(ctx, Window, JSStringCreateWithUTF8CString("Direction"), Window_Direction, kJSPropertyAttributeNone, NULL);
-       
-       functionWrapper(ctx, Window, "SetProperty", (JSObjectCallAsFunctionCallback)jsWindowCreate);
-       functionWrapper(ctx, Window, "Show", (JSObjectCallAsFunctionCallback)jsWindowShow);     
-       
-       classWrapper(ctx, jobjGlobal, "xawControl", &Widget);
-       functionWrapper(ctx, Widget, "Create", (JSObjectCallAsFunctionCallback)jsControlCreate);
-       JSObjectRef Control_Name, Control_Type, Control_Parent, Control_Text, Control_Width, Control_Height, Control_Left, Control_Top, Control_Border, Control_Callback;
-       Control_Name = JSValueMakeString(ctx, JSStringCreateWithUTF8CString("undefined"));
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Name"), Control_Name, kJSPropertyAttributeNone, NULL);  
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Type"), Control_Type, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Parent"), Control_Parent, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Text"), Control_Text, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Width"), Control_Width, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Height"), Control_Height, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Left"), Control_Left, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Top"), Control_Top, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Border"), Control_Border, kJSPropertyAttributeNone, NULL);
-       JSObjectSetProperty(ctx, Widget, JSStringCreateWithUTF8CString("Callback"), Control_Callback, kJSPropertyAttributeNone, NULL);
+       assert(sizeof( ( (void*)1 ) )  >=  sizeof( (int)1 ) );
+       JSObjectRef Window, Form;
+       classWrapper(ctx, jobjGlobal, "xawWindow", &Window, jsXawWindowConstructor);\
+       classWrapper(ctx, jobjGlobal, "xawForm", &Form, jsXawFormConstructor);
+       classWrapper(ctx, jobjGlobal, "xawWidget", &Form, jsXawWidgetConstructor);
+       
+       functionWrapper(ctx, jobjGlobal, "setTimeout", (JSObjectCallAsFunctionCallback)jsXawAppAddTimeOut);
+       
 }
diff --git a/xaw.h b/xaw.h
index 703f3f6..19909ca 100644 (file)
--- a/xaw.h
+++ b/xaw.h
@@ -9,8 +9,11 @@
 #include <X11/Xaw/Command.h>
 #include <X11/Xaw/AsciiText.h>
 
+#include <assert.h>
+
 #include "js.h"
 
+
 typedef struct  /* XawのWindowを構造体として定義 */
 {
        XtAppContext* Context;
@@ -47,7 +50,7 @@ typedef struct  /* XawのWidetを構造体として定義 */
 
 typedef struct  /* JSにイベントを渡すための構造体 */
 {
-       JSGlobalContextRef Context;
+       JSGlobalContextRef* Context;
        JSObjectRef Function;
 } jsCallbackInfo;