diff --git a/Examples/javascript/variables/Makefile b/Examples/javascript/variables/Makefile index ea41e55..e80e370 100755 --- a/Examples/javascript/variables/Makefile +++ b/Examples/javascript/variables/Makefile @@ -1,6 +1,6 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig -SRCS = example.cpp +SRCS = example.c JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript JAVASCRIPT_MODULE = example @@ -11,7 +11,7 @@ SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc all:: $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ -SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp +SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript clean:: $(MAKE) -f $(TOP)/Makefile javascript_clean diff --git a/Examples/javascript/variables/example.cpp b/Examples/javascript/variables/example.c similarity index 100% rename from Examples/javascript/variables/example.cpp rename to Examples/javascript/variables/example.c diff --git a/Examples/javascript/variables/runme.js b/Examples/javascript/variables/runme.js index bfefd7f..7c4e151 100755 --- a/Examples/javascript/variables/runme.js +++ b/Examples/javascript/variables/runme.js @@ -1,7 +1,5 @@ - // file: runme.js - // Try to set the values of some global variables example.ivar = 42; example.svar = -31000; @@ -36,8 +34,8 @@ print("strvar = " + example.strvar+ "\n"); print("cstrvar = " + example.cstrvar+ "\n"); print("iptrvar = " + example.iptrvar+ "\n"); print("name = " + example.name + "\n"); -print("ptptr = " + example.ptptr + example.Point_print(ptptr) + "\n"); -print("pt = " + pt + "," + Point_print(pt) + "\n"); +print("ptptr = " + example.ptptr + ": " + example.Point_print(example.ptptr) + "\n"); +print("pt = " + example.pt + ": " + example.Point_print(example.pt) + "\n"); print("\nVariables (values printed from C)"); @@ -53,17 +51,18 @@ try{ } catch(e){ print("Good."); +} print("Trying to set 'status'"); try{ example.status = 0; print("Hey, what's going on?!?! This shouldn't work"); - } -catch(e){ +} catch(e){ print("Good."); } + print("\nI'm going to try and update a structure variable.\n"); example.pt = example.ptptr; -print("The new value is"); +print("The new value is: "); example.pt_print(); -print("You should see the value" + example.Point_print(example.ptptr)); +print("You should see the value: " + example.Point_print(example.ptptr)); diff --git a/Lib/javascript/jsc/javascripthelpers.swg b/Lib/javascript/jsc/javascripthelpers.swg index 0c18cc6..86d6323 100644 --- a/Lib/javascript/jsc/javascripthelpers.swg +++ b/Lib/javascript/jsc/javascripthelpers.swg @@ -1,4 +1,4 @@ -%insert(runtime) %{ +%insert(wrapper) %{ bool JS_registerClass(JSGlobalContextRef context, JSObjectRef parentObject, const char* className, @@ -38,4 +38,22 @@ bool JS_registerFunction(JSGlobalContextRef context, JSObjectRef object, JSStringRelease(js_functionName); return true; } + +bool JS_veto_set_variable(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) +{ + char buffer[256]; + char msg[512]; + int res; + + JSStringGetUTF8CString(propertyName, buffer, 256); + res = sprintf(msg, "Tried to write read-only variable: %s.", buffer); + + if(res<0) { + SWIG_exception_fail(SWIG_ERROR, "Tried to write read-only variable."); + } else { + SWIG_exception_fail(SWIG_ERROR, msg); + } + + return false; +} %} diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index 289c554..1dbd8f4 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -244,6 +244,7 @@ class JSCEmitter:public JSEmitter { File *f_init; String *NULL_STR; + String *VETO_SET; const char *GLOBAL_STR; // contains context specific structs @@ -538,11 +539,12 @@ Template & Template::replace(const String *pattern, const String *repl) { } JSCEmitter::JSCEmitter() -: JSEmitter(), NULL_STR(NewString("NULL")) { +: JSEmitter(), NULL_STR(NewString("NULL")), VETO_SET(NewString("JS_veto_set_variable")) { } JSCEmitter::~JSCEmitter() { Delete(NULL_STR); + Delete(VETO_SET); } /* --------------------------------------------------------------------- @@ -810,7 +812,7 @@ int JSCEmitter::exitFunction(Node *n) { int JSCEmitter::enterVariable(Node *n) { current_getter = NULL_STR; - current_setter = NULL_STR; + current_setter = VETO_SET; current_propertyname = Swig_scopename_last(Getattr(n, "name")); is_immutable = GetFlag(n, "wrap:immutable"); @@ -824,7 +826,6 @@ int JSCEmitter::exitVariable(Node *n) { .replace("${getname}", current_getter) .replace("${propertyname}", current_propertyname); - if (GetFlag(n, "ismember")) { if (Equal(Getattr(n, "storage"), "static") || (Equal(Getattr(n, "nodeType"), "enumitem"))) {