r8220: added auto-generation of ENUM constants in ejs wrapper. So we can now use...
authorAndrew Tridgell <tridge@samba.org>
Fri, 8 Jul 2005 04:55:07 +0000 (04:55 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:19:21 +0000 (13:19 -0500)
instead of a integer in ejs scripts making rpc calls

source/build/pidl/ejs.pm
source/scripting/ejs/ejsrpc.c
source/scripting/ejs/ejsrpc.h
source/scripting/ejs/smbcalls.c
source/scripting/ejs/smbcalls_rpc.c
source/scripting/ejs/smbscript.c
testprogs/ejs/echo.js

index 3fe9579abaa0dd2adcaf029a93079da1f8c9d647..24204b79b3d30be717c094aaaf02479d7c17dd73 100644 (file)
@@ -10,6 +10,7 @@ use strict;
 use pidl::typelist;
 
 my($res);
+my %constants;
 
 sub pidl ($)
 {
@@ -409,6 +410,17 @@ sub EjsEnumPush($$)
 {
        my $name = shift;
        my $d = shift;
+       my $v = 0;
+       # put the enum elements in the constants array
+       foreach my $e (@{$d->{ELEMENTS}}) {
+               chomp $e;
+               if ($e =~ /^(.*)=\s*(.*)\s*$/) {
+                       $e = $1;
+                       $v = $2;
+               }
+               $constants{$e} = $v;
+               $v++;
+       }
        pidl "\nstatic NTSTATUS ejs_push_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, const enum $name *r)\n{\n";
        pidl "\tunsigned e = *r;\n";
        pidl "\tNDR_CHECK(ejs_push_enum(ejs, v, name, &e));\n";
@@ -420,7 +432,9 @@ sub EjsEnumPush($$)
 # push a bitmap
 sub EjsBitmapPush($$)
 {
-       # ignored for now
+       my $name = shift;
+       my $e = shift;
+#      print util::MyDumper($e);
 }
 
 
@@ -486,6 +500,8 @@ sub EjsInterface($)
        my @fns = ();
        my $name = $interface->{NAME};
 
+       %constants = ();
+
        foreach my $d (@{$interface->{TYPEDEFS}}) {
                EjsTypedefPush($d);
                EjsTypedefPull($d);
@@ -506,6 +522,18 @@ sub EjsInterface($)
        foreach (@fns) {
                pidl "\tejsDefineCFunction(-1, \"dcerpc_$_\", ejs_$_, NULL, MPR_VAR_SCRIPT_HANDLE);\n";
        }
+       pidl "}\n\n";
+
+       pidl "void setup_ejs_constants_$name(int eid)\n";
+       pidl "{\n";
+       foreach my $v (keys %constants) {
+               my $value = $constants{$v};
+               if (substr($value, 0, 1) eq "\"") {
+                       pidl "\tejs_set_constant_string(eid, \"$v\", $value);\n";
+               } else {
+                       pidl "\tejs_set_constant_int(eid, \"$v\", $value);\n";
+               }
+       }
        pidl "}\n";
 }
 
index 19c1a2fc8d81df52377325cbee57acf92cb5de6d..6536b94ca4b88afa6e2fe0f1285687fc1895faee 100644 (file)
@@ -273,7 +273,8 @@ NTSTATUS ejs_pull_string(struct ejs_rpc *ejs,
                DEBUG(1,("ejs_pull_string: unable to find '%s'\n", name));
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
-       *s = mprToString(var);
+       *s = talloc_strdup(ejs, mprToString(var));
+       NT_STATUS_HAVE_NO_MEMORY(*s);
        return NT_STATUS_OK;
 }
 
@@ -285,3 +286,21 @@ NTSTATUS ejs_push_string(struct ejs_rpc *ejs,
 {
        return mprSetVar(v, name, mprCreateStringVar(s, True));
 }
+
+/*
+  setup a constant int
+*/
+void ejs_set_constant_int(int eid, const char *name, int value)
+{
+       struct MprVar *v = ejsGetGlobalObject(eid);
+       mprSetVar(v, name, mprCreateIntegerVar(value));
+}
+
+/*
+  setup a constant string
+*/
+void ejs_set_constant_string(int eid, const char *name, const char *value)
+{
+       struct MprVar *v = ejsGetGlobalObject(eid);
+       mprSetVar(v, name, mprCreateStringVar(value, False));
+}
index fdf15c027cb91f53a1523697b136bca894d50b9c..0435b523657e89d0f6a1d800df51a11a52f4d527 100644 (file)
@@ -68,6 +68,8 @@ NTSTATUS ejs_pull_string(struct ejs_rpc *ejs,
                         struct MprVar *v, const char *name, char **s);
 NTSTATUS ejs_push_string(struct ejs_rpc *ejs, 
                         struct MprVar *v, const char *name, const char *s);
+void ejs_set_constant_int(int eid, const char *name, int value);
+void ejs_set_constant_string(int eid, const char *name, const char *value);
 
 #define EJS_ALLOC_SIZE(ejs, s, size) do { \
   (s) = talloc_size(ejs, size); \
index 041bd59f1a120f70a281e85732997e3bd892f24c..0f43da349daedffc5ba28ba41149f96bfe99c641 100644 (file)
@@ -198,3 +198,11 @@ void smb_setup_ejs_functions(void)
        ejsDefineStringCFunction(-1, "getDomainList", ejs_domain_list, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
 }
+
+/*
+  setup constants that can be used from ejs
+*/
+void smb_setup_ejs_constants(int eid)
+{
+       smb_setup_ejs_rpc_constants(eid);
+}
index 6ec3e29e506a3ed82b4c1255b0578c8a51f84c39..f8ea37b754a68953639b42a2735911c260310cba 100644 (file)
@@ -173,3 +173,12 @@ void smb_setup_ejs_rpc(void)
        ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
        setup_ejs_rpcecho();
 }
+
+/*
+  setup constants for rpc calls
+*/
+void smb_setup_ejs_rpc_constants(int eid)
+{
+       void setup_ejs_constants_rpcecho(int);
+       setup_ejs_constants_rpcecho(eid);
+}
index aa0fc42c4875d7892f728dae98bd6227830ba350..244ae0b42aafc72928a795605646b0a66f7e8c44 100644 (file)
@@ -88,6 +88,8 @@ void ejs_exception(const char *reason)
                exit(127);
        }
 
+       smb_setup_ejs_constants(eid);
+
        /* setup ARGV[] in the ejs environment */
        for (i=1;argv[i];i++) {
                argv_list = str_list_add(argv_list, argv[i]);
index 4bc8fdefb1ceb5a030cca4a0f6dcd12697469911..df12e4648671a6e58f175d62b7dad68fc8ea5cf9 100644 (file)
@@ -185,18 +185,18 @@ function test_TestEnum(conn)
 
        print("Testing echo_TestEnum\n");
 
-       io.input.foo1 = 1;
+       io.input.foo1 = ECHO_ENUM1;
        io.input.foo2 = new Object();
-       io.input.foo2.e1 = 1;
-       io.input.foo2.e2 = 1;
+       io.input.foo2.e1 = ECHO_ENUM1;
+       io.input.foo2.e2 = ECHO_ENUM1_32;
        io.input.foo3 = new Object();
-       io.input.foo3.e1 = 2;
+       io.input.foo3.e1 = ECHO_ENUM2;
        status = dcerpc_echo_TestEnum(conn, io);
        check_status_ok(status);
-       assert(io.output.foo1    == 1);
-       assert(io.output.foo2.e1 == 2);
-       assert(io.output.foo2.e2 == 1);
-       assert(io.output.foo3.e1 == 2);
+       assert(io.output.foo1    == ECHO_ENUM1);
+       assert(io.output.foo2.e1 == ECHO_ENUM2);
+       assert(io.output.foo2.e2 == ECHO_ENUM1_32);
+       assert(io.output.foo3.e1 == ECHO_ENUM2);
 }
 
 /*