Javascript examples.
This commit is contained in:
parent
ecf9f96079
commit
48af60d829
76 changed files with 1685 additions and 0 deletions
21
Examples/javascript/variables/Makefile
Executable file
21
Examples/javascript/variables/Makefile
Executable file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
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
|
||||
|
||||
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);
|
||||
}
|
||||
6
Examples/javascript/variables/example.h
Executable file
6
Examples/javascript/variables/example.h
Executable file
|
|
@ -0,0 +1,6 @@
|
|||
/* File: example.h */
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
} Point;
|
||||
|
||||
49
Examples/javascript/variables/example.i
Executable file
49
Examples/javascript/variables/example.i
Executable file
|
|
@ -0,0 +1,49 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Some global variable declarations */
|
||||
%inline %{
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
extern unsigned int uivar;
|
||||
extern unsigned short usvar;
|
||||
extern unsigned long ulvar;
|
||||
extern signed char scvar;
|
||||
extern unsigned char ucvar;
|
||||
extern char cvar;
|
||||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
extern const char cstrvar[];
|
||||
extern int *iptrvar;
|
||||
extern char name[256];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
%}
|
||||
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
|
||||
%inline %{
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%}
|
||||
|
||||
%mutable;
|
||||
|
||||
/* Some helper functions to make it easier to test */
|
||||
%inline %{
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
%}
|
||||
|
||||
68
Examples/javascript/variables/runme.js
Executable file
68
Examples/javascript/variables/runme.js
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
var example = require("./build/Release/example");
|
||||
|
||||
// Try to set the values of some global variables
|
||||
example.ivar = 42;
|
||||
example.svar = -31000;
|
||||
example.lvar = 65537;
|
||||
example.uivar = 123456;
|
||||
example.usvar = 61000;
|
||||
example.ulvar = 654321;
|
||||
example.scvar = -13;
|
||||
example.ucvar = 251;
|
||||
example.cvar = "S";
|
||||
example.fvar = 3.14159;
|
||||
example.dvar = 2.1828;
|
||||
example.strvar = "Hello World";
|
||||
example.iptrvar= example.new_int(37);
|
||||
example.ptptr = example.new_Point(37,42);
|
||||
example.name = "Bill";
|
||||
|
||||
// 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");
|
||||
|
||||
|
||||
console.log("\nVariables (values console.loged from C)");
|
||||
|
||||
example.print_vars();
|
||||
|
||||
console.log("\nNow I'm going to try and modify some read only variables");
|
||||
|
||||
console.log("Tring to set 'path'");
|
||||
try{
|
||||
example.path = "Whoa!";
|
||||
console.log("Hey, what's going on?!?! This shouldn't work");
|
||||
}
|
||||
catch(e){
|
||||
console.log("Good.");
|
||||
}
|
||||
|
||||
console.log("Trying to set 'status'");
|
||||
try{
|
||||
example.status = 0;
|
||||
console.log("Hey, what's going on?!?! This shouldn't work");
|
||||
} catch(e){
|
||||
console.log("Good.");
|
||||
}
|
||||
|
||||
console.log("\nI'm going to try and update a structure variable.\n");
|
||||
example.pt = example.ptptr;
|
||||
console.log("The new value is: ");
|
||||
example.pt_print();
|
||||
console.log("You should see the value: " + example.Point_print(example.ptptr));
|
||||
9
Examples/javascript/variables/swig_gdb.log
Normal file
9
Examples/javascript/variables/swig_gdb.log
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Loaded swig printers
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
SwigStringPrinter: Could not convert const char* to string
|
||||
Loading…
Add table
Add a link
Reference in a new issue