Convert javascript examples to nodejs.
This commit is contained in:
parent
dbf2b1fe94
commit
5da54ca435
4 changed files with 312 additions and 223 deletions
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_wrapper_cpp
|
||||
|
||||
build:: wrapper
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_build
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/variables/binding.gyp
Normal file
8
Examples/javascript/variables/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
96
Examples/javascript/variables/example.cxx
Executable file
96
Examples/javascript/variables/example.cxx
Executable file
|
|
@ -0,0 +1,96 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* I'm a file containing some C global variables */
|
||||
|
||||
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
|
||||
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
|
||||
# define _CRT_SECURE_NO_DEPRECATE
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "example.h"
|
||||
|
||||
int ivar = 0;
|
||||
short svar = 0;
|
||||
long lvar = 0;
|
||||
unsigned int uivar = 0;
|
||||
unsigned short usvar = 0;
|
||||
unsigned long ulvar = 0;
|
||||
signed char scvar = 0;
|
||||
unsigned char ucvar = 0;
|
||||
char cvar = 0;
|
||||
float fvar = 0;
|
||||
double dvar = 0;
|
||||
char *strvar = 0;
|
||||
#ifdef __cplusplus // Note: for v8 this must be linkable with g++, without extern cstrvar is mangled
|
||||
extern const char cstrvar[] = "Goodbye";
|
||||
#else
|
||||
const char cstrvar[] = "Goodbye";
|
||||
#endif
|
||||
const
|
||||
int *iptrvar = 0;
|
||||
char name[256] = "Dave";
|
||||
char path[256] = "/home/beazley";
|
||||
|
||||
|
||||
/* Global variables involving a structure */
|
||||
Point *ptptr = 0;
|
||||
Point pt = { 10, 20 };
|
||||
|
||||
/* A variable that we will make read-only in the interface */
|
||||
int status = 1;
|
||||
|
||||
/* A debugging function to print out their values */
|
||||
|
||||
void print_vars() {
|
||||
printf("ivar = %d\n", ivar);
|
||||
printf("svar = %d\n", svar);
|
||||
printf("lvar = %ld\n", lvar);
|
||||
printf("uivar = %u\n", uivar);
|
||||
printf("usvar = %u\n", usvar);
|
||||
printf("ulvar = %lu\n", ulvar);
|
||||
printf("scvar = %d\n", scvar);
|
||||
printf("ucvar = %u\n", ucvar);
|
||||
printf("fvar = %g\n", fvar);
|
||||
printf("dvar = %g\n", dvar);
|
||||
printf("cvar = %c\n", cvar);
|
||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||
printf("iptrvar = %p\n", iptrvar);
|
||||
printf("name = %s\n", name);
|
||||
printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
|
||||
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||
printf("status = %d\n", status);
|
||||
}
|
||||
|
||||
/* A function to create an integer (to test iptrvar) */
|
||||
|
||||
int *new_int(int value) {
|
||||
int *ip = (int *) malloc(sizeof(int));
|
||||
*ip = value;
|
||||
return ip;
|
||||
}
|
||||
|
||||
/* A function to create a point */
|
||||
|
||||
Point *new_Point(int x, int y) {
|
||||
Point *p = (Point *) malloc(sizeof(Point));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
char * Point_print(Point *p) {
|
||||
static char buffer[256];
|
||||
if (p) {
|
||||
sprintf(buffer,"(%d,%d)", p->x,p->y);
|
||||
} else {
|
||||
sprintf(buffer,"null");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void pt_print() {
|
||||
printf("(%d, %d)\n", pt.x, pt.y);
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
// file: runme.js
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
// Try to set the values of some global variables
|
||||
example.ivar = 42;
|
||||
|
|
@ -17,52 +17,52 @@ example.iptrvar= example.new_int(37);
|
|||
example.ptptr = example.new_Point(37,42);
|
||||
example.name = "Bill";
|
||||
|
||||
// Now print out the values of the variables
|
||||
print("Variables (values printed from Python)" + "\n");
|
||||
print("ivar = " + example.ivar + "\n");
|
||||
print("svar = " + example.svar + "\n");
|
||||
print("lvar = " + example.lvar + "\n");
|
||||
print("uivar = " + example.uivar + "\n");
|
||||
print("usvar = " + example.usvar + "\n");
|
||||
print("ulvar = " + example.ulvar + "\n");
|
||||
print("scvar = " + example.scvar + "\n");
|
||||
print("ucvar = " + example.ucvar + "\n");
|
||||
print("fvar = " + example.fvar + "\n");
|
||||
print("dvar = " + example.dvar + "\n");
|
||||
print("cvar = " + example.cvar + "\n");
|
||||
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(example.ptptr) + "\n");
|
||||
print("pt = " + example.pt + ": " + example.Point_print(example.pt) + "\n");
|
||||
// Now console.log out the values of the variables
|
||||
console.log("Variables (values console.loged from Python)" + "\n");
|
||||
console.log("ivar = " + example.ivar + "\n");
|
||||
console.log("svar = " + example.svar + "\n");
|
||||
console.log("lvar = " + example.lvar + "\n");
|
||||
console.log("uivar = " + example.uivar + "\n");
|
||||
console.log("usvar = " + example.usvar + "\n");
|
||||
console.log("ulvar = " + example.ulvar + "\n");
|
||||
console.log("scvar = " + example.scvar + "\n");
|
||||
console.log("ucvar = " + example.ucvar + "\n");
|
||||
console.log("fvar = " + example.fvar + "\n");
|
||||
console.log("dvar = " + example.dvar + "\n");
|
||||
console.log("cvar = " + example.cvar + "\n");
|
||||
console.log("strvar = " + example.strvar+ "\n");
|
||||
console.log("cstrvar = " + example.cstrvar+ "\n");
|
||||
console.log("iptrvar = " + example.iptrvar+ "\n");
|
||||
console.log("name = " + example.name + "\n");
|
||||
console.log("ptptr = " + example.ptptr + ": " + example.Point_print(example.ptptr) + "\n");
|
||||
console.log("pt = " + example.pt + ": " + example.Point_print(example.pt) + "\n");
|
||||
|
||||
|
||||
print("\nVariables (values printed from C)");
|
||||
console.log("\nVariables (values console.loged from C)");
|
||||
|
||||
example.print_vars();
|
||||
|
||||
print("\nNow I'm going to try and modify some read only variables");
|
||||
console.log("\nNow I'm going to try and modify some read only variables");
|
||||
|
||||
print("Tring to set 'path'");
|
||||
console.log("Tring to set 'path'");
|
||||
try{
|
||||
example.path = "Whoa!";
|
||||
print("Hey, what's going on?!?! This shouldn't work");
|
||||
console.log("Hey, what's going on?!?! This shouldn't work");
|
||||
}
|
||||
catch(e){
|
||||
print("Good.");
|
||||
console.log("Good.");
|
||||
}
|
||||
|
||||
print("Trying to set 'status'");
|
||||
console.log("Trying to set 'status'");
|
||||
try{
|
||||
example.status = 0;
|
||||
print("Hey, what's going on?!?! This shouldn't work");
|
||||
console.log("Hey, what's going on?!?! This shouldn't work");
|
||||
} catch(e){
|
||||
print("Good.");
|
||||
console.log("Good.");
|
||||
}
|
||||
|
||||
print("\nI'm going to try and update a structure variable.\n");
|
||||
console.log("\nI'm going to try and update a structure variable.\n");
|
||||
example.pt = example.ptptr;
|
||||
print("The new value is: ");
|
||||
console.log("The new value is: ");
|
||||
example.pt_print();
|
||||
print("You should see the value: " + example.Point_print(example.ptptr));
|
||||
console.log("You should see the value: " + example.Point_print(example.ptptr));
|
||||
|
|
|
|||
Loading…
Reference in a new issue