Skip to content

Commit

Permalink
Fix bugs revealed by variables example.
Browse files Browse the repository at this point in the history
  • Loading branch information
neha1 committed Aug 20, 2012
1 parent a056f98 commit 853aee8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions 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
Expand All @@ -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
Expand Down
File renamed without changes.
15 changes: 7 additions & 8 deletions 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;
Expand Down Expand Up @@ -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)");
Expand All @@ -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));
20 changes: 19 additions & 1 deletion Lib/javascript/jsc/javascripthelpers.swg
@@ -1,4 +1,4 @@
%insert(runtime) %{
%insert(wrapper) %{

bool JS_registerClass(JSGlobalContextRef context, JSObjectRef parentObject,
const char* className,
Expand Down Expand Up @@ -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;
}
%}
7 changes: 4 additions & 3 deletions Source/Modules/javascript.cxx
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/* ---------------------------------------------------------------------
Expand Down Expand Up @@ -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");

Expand All @@ -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"))) {

Expand Down

0 comments on commit 853aee8

Please sign in to comment.