Merge branch 'javascript'
* javascript: (342 commits) Revert "Remove unnecessary Javascript installs on Travis" Remove unnecessary Javascript installs on Travis Javascript cosmetic changes Javascript: ensure banner appears before %begin code Enable Javascript in director_nspace_director_name_collision.i. Make javascript 'nspace' conform to corresponding lua example. Added some documentation on using 'node-webkit'. Enable javascript examples 'exception', 'namespace'. Fix bug and regression in javascript namespace generator. Prettify generated output in JS emitters. Fix node-gyp configuration for example 'exception'. Fix regression of 226da4. Print error when specified multiple js engines. Removed obsolete 'node.i'. Added a comment about V8_VERSION macro. Restore missing copyright info in javascript branch beautify javascript.cxx Javascript variable naming convention corrections Javascript test-suite - warning suppression not needed Javascript example Makefiles more consistent with other languages Cosmetic code changes in javascript.cxx Fix regression in Javascript generator for enums. Fix configuration for Javascript/node ctests. SKIP_JAVASCRIPT only when there is none of v8/node/jsc. Fix generator for Javascript enums. Turn on Swig_cparse_cplusplusout for Javascript/v8. Fix configuration for ctest with Javascript/v8. Add cast to test 'string_simple' to avoid C++ compiler error. Add 'fix' for Javascript/v8 to test 'enum_forward'. Only disable javascript tests if node-gyp is missing when testing node Revert "Javascript node test-suite makefile fixes for parallel make" Suppress keyword warning in test-suite Restore and fix infinity testcase for Javascript Simple C string test Javascript node test-suite makefile fixes for parallel make Restore missing make code in javascript branch Javascript test-suite makefile update ADD RUNPIPE and RUNTOOL to Javascript examples Remove unnecessary blank lines from Javascript examples Detect node-gyp at configure time and show version in javascript_version target Fix typo in Javascript exception Revert css style changes in javascript branch Add missing license info Javascript html documentation tidy up Improve missing JS engine error message a few Javascript clarifications in configure.ac Makefile and autoconf file tidy up for Javascript Tidy up Example makefile for Javascript Alphabetical order for Javascript in -help output Remove junk files Travis file code consistency Turn on Travis testing on javascript branch Add javascript to Sections.html Optimized travis before_install for Javascript. Document Javascript related preprocessor macros. Updated Javascript documentation. Insert a workaround for c_delete and c_delete_function for v8. Update Javascript.html. Remove inadvertently added xcode file. Remove gdb pretty printing. Remove cmake relict. Remove pandoc toolchain. Remove Javascript documentation source file. Revert "Development version for travis configuration." Fix travis configuration. Fix regression in custom v8 interpreter. Development version for travis configuration. Generalized smoke test configuration. Add 'version' target for javascript. Merged branch 'devel' from https://github.com/oliver----/swig-v8 (squashed commit): Add check to skip javascript test-suite if default interpreter is not installed. Activate travis matrix for different Javascript interpreters. Beautified Javascript module. Fix regressions. Removed obsolete paragraph from Javascript documentation. Fix pandoc filter. Minor tweaks in Javascript documentation. Adding a chapter about Javascript tests and examples. Minor change in Javascript configuration. Fix issues that were observed with newer JavascriptCore. Add missing copy ctor for Javascript Code Template class. Removed CMake. Fix regression in Javascript configuration. Fix regressions in configuration of some Javascript examples. More gitignores filtering Javascript related files. Fix custom javascript interpreter configuration for OSX. Make examples work with node.js and the custom interpreter. Introduced an extra Makefile for the custom javascript interpreter. Fix travis configuration. Added a comment. Fix regressions in Javascript example configuration. Fix issue with strange constants under OSX. Some fixes in example section of Javascript documentation. Add note about '-node' command line flag for Node.js code generation. Added examples to Javascript module documentation. Simplification in common javascript example Makefile. Add an extra argument for enabling nodejs support. Add more notes about how to install node. Better error message when no Javascript emitter is specified. Deactivated broken JS examples. Refactored configuration for javascript examples. Beginning to document v8 integration. Documented extending Node.js and Webkit's Javascript engine. Pandoc filter detects shell blocks when not explicitely given. Conitnued documenting the Javascript module. Not a real change. Add a pandoc filter to create compatible html. Add Javascript to chapters. More Javascript module documentation. Next iteration on creating a documentation for the Javascript module. Next iteration on creating a documentation for the Javascript module. Travis configuration for Javascript branch. Javascript examples. Javascript documentation. CMake configuration. Fix regression. Fix test configuration. Trying to install former working version of nodejs. Fix test configuration for nodejs. Add stub std_deque.i files. Add non pretty printing function to Templates. Fix bug with typedefd function pointers. Add some test runners. Fix emitter for member pointer constants. Fix %nspace support and activated relevant tests. Disable warnings for some v8 test-cases. Fix regression. Bugfix in argument marshalling. Disable testcase 'typemap_variables' for v8. Bugfix for Javascript generator. Replace $symname in generated function wrappers. Add missing macros. Let v8 generated code include stdlib.h Add cdata.i typemaps. Rearrange generation of init block to have custom init code within the initializer body. Make JSC inheritance definition more robust. Removed dead code. Bugfix for JSC %typemap(out) std::string&. Fix typemap declarations for (unsigned) long long. Add support for IN/OUTPUT typemaps. removed Clear before Dispose from newer v8 code, consider the following code: template <class T> void Persistent<T>::Dispose() { if (this->IsEmpty()) return; //Clear will trigger this V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_)); ... } fixed newer v8 compilation Add stub 'typemaps.i' files. Bugfix Javascript generator: valid name for dispatcher functions. Fix Javascript generator for smartpointeraccessed variables. Bugfix for Javascript generator: avoid duplicate action code generation. Bugfix in Javascript generator: detect member setter/getters correctly. Fix configuration for nodejs based tests. Some fixes and cleanup in Javascript generator. Fix settings for building nodejs tests. Add support for PackedData to Javascript generator. Added two more Javascript tests. Some fixes for the Javascript generator. Redefined set of Javascript smoke tests. Clean up in `javascripttypemaps.swg`. Fix v8 generator to use a non clashing name for built-in 'equals' method. Fix Javascript generator to use %renamed variable names. Activate Javascript test-cases `rename_simple` and `rename_scope`. Activate Javascript testcases `rename1-4`. Add `infinity` test-case to list of smoke tests. Added unit test using C99 INFINITY. This test actually tests a float conversion bug where converting to float imposed overflow checking which should not be there and causes this program to error out. This was seen originally in Javascript, but it turns out Python has the same bug. Lua does not have this bug. Other generators have not been tested. Bug-fix for static variables as proposed by Kota Iguchi. Let Travis test all three targets. Allow to run the test suite with our list of smoke tests. Javascript V8 test-cases always create cpp wrappers. Test-suite Makefile now supports testing for all three Javascript variants. Rewritten Javascript autoconfiguration. Renamed object provided to JS initializers. Refactored custom javascript engines to support primitive 'require' statements. fixed deprecation warnings for v8-3.21 moved common swig v8 definitions in javascriptruntime.swg, fixed obsoleted api calls Added missing template configuration files for nodejs based tests. Put the SWIG_V8_VERSION macro into "runtime" block. Use a diffent name for the V8_VERSION provided via command line. fixed return values and added missing newer v8 dtor wrapper. Allow exceptions in generated example/test node extensions. Fix cleanup configuration for javascript test-suite. Relax type check in SWIG_AsVal_int. Fixing travis configuration. Remove javascript autoconf detection (Temporarily). Fix in travis config. Not a real change. Slight modification to travis config. Add a travis configuration (experimental). Make javascript test-suite work with nodejs. Replace old style v8 version switches. Fix error in SWIG_V8_GetInstancePtr. Add "equals" to compare between pointers Add "getCPtr" to retrieve pointer value Typemap for natural support for arrays Added the finalize callback (JSObjectFinalizeCallback) Fix regression. Convert javascript examples to nodejs. Ignore javascript Example build files. First example that addresses node.js as primary execution environment. Better v8 version handling. Add a dedicated mode for creating node modules. Starting from scratch with Examples configuration. ported to newer, more efficient v8 api Patch to support argout typemap for your swig-v8 branch. Since this code uses assert, #include <assert.h> is needed. ...
This commit is contained in:
commit
98237e2b81
225 changed files with 11713 additions and 53 deletions
8
.gitignore
vendored
8
.gitignore
vendored
|
|
@ -5,6 +5,8 @@
|
|||
# Editor files and various other junk
|
||||
.*.sw?
|
||||
*.bak
|
||||
*.log
|
||||
.DS_Store
|
||||
|
||||
# Local PCRE
|
||||
pcre
|
||||
|
|
@ -23,6 +25,7 @@ pcre
|
|||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
*.bundle
|
||||
|
||||
# C/C++ static libraries, based on:
|
||||
# https://github.com/github/gitignore/blob/master/C.gitignore
|
||||
|
|
@ -71,6 +74,7 @@ Examples/xml/Makefile
|
|||
Source/Include/stamp-h1
|
||||
Source/Include/swigconfig.h
|
||||
Source/Makefile
|
||||
Tools/javascript/Makefile
|
||||
.deps
|
||||
config.log
|
||||
config.status
|
||||
|
|
@ -86,6 +90,7 @@ Source/CParse/parser.c
|
|||
Source/CParse/parser.h
|
||||
Source/eswig
|
||||
swig
|
||||
Tools/javascript/javascript
|
||||
|
||||
# Generated documentation
|
||||
Doc/Manual/CCache.html
|
||||
|
|
@ -103,6 +108,7 @@ Examples/test-suite/d/*/
|
|||
Examples/test-suite/go/*/
|
||||
Examples/test-suite/guile/*/
|
||||
Examples/test-suite/java/*/
|
||||
Examples/test-suite/javascript/*/
|
||||
Examples/test-suite/lua/*/
|
||||
Examples/test-suite/mzscheme/*/
|
||||
Examples/test-suite/ocaml/*/
|
||||
|
|
@ -115,6 +121,8 @@ Examples/test-suite/r/*/
|
|||
Examples/test-suite/ruby/*/
|
||||
Examples/test-suite/tcl/*/
|
||||
Examples/test-suite/uffi/*/
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
|
||||
# Python generated files, based on:
|
||||
# https://github.com/github/gitignore/blob/master/Python.gitignore
|
||||
|
|
|
|||
46
.travis.yml
46
.travis.yml
|
|
@ -1,45 +1,17 @@
|
|||
language: cpp
|
||||
language: c
|
||||
compiler:
|
||||
- clang
|
||||
- gcc
|
||||
- clang
|
||||
env:
|
||||
- SWIGLANG=
|
||||
matrix:
|
||||
include:
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=csharp
|
||||
env: SWIGLANG=javascript ENGINE=node
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=go
|
||||
env: SWIGLANG=javascript ENGINE=jsc
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=guile
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=java
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=lua
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=octave SWIGJOBS=-j4
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=perl5
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=php
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python VER=2.4
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python VER=2.5
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python VER=2.6
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python # 2.7
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python PY3=3 # 3.2
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python PY3=3 VER=3.3
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=python PY3=3 VER=3.4
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=ruby
|
||||
- compiler: gcc
|
||||
env: SWIGLANG=tcl
|
||||
env: SWIGLANG=javascript ENGINE=v8
|
||||
allow_failures:
|
||||
# None
|
||||
before_install:
|
||||
|
|
@ -51,15 +23,18 @@ before_install:
|
|||
- if test -z "$SWIGLANG"; then sudo apt-get -qq install yodl; fi
|
||||
- if test "$SWIGLANG" = "csharp"; then sudo apt-get -qq install mono-devel; fi
|
||||
- if test "$SWIGLANG" = "go"; then go env | sed -e 's/^/export /' > goenvsetup && source goenvsetup && rm -f goenvsetup; fi # Until configure.ac is fixed
|
||||
- if test "$SWIGLANG" = "javascript" -a "$ENGINE" = "node"; then sudo apt-get install -qq rlwrap python-software-properties && echo 'yes' | sudo add-apt-repository ppa:chris-lea/node.js && sudo apt-get -qq update && sudo apt-get install -qq nodejs && sudo npm install -g node-gyp; fi
|
||||
- if test "$SWIGLANG" = "javascript" -a "$ENGINE" = "jsc"; then sudo apt-get install -qq libwebkitgtk-dev; fi
|
||||
- if test "$SWIGLANG" = "javascript" -a "$ENGINE" = "v8"; then sudo apt-get install -qq libv8-dev; fi
|
||||
- if test "$SWIGLANG" = "guile"; then sudo apt-get -qq install guile-2.0-dev; fi
|
||||
- if test "$SWIGLANG" = "lua"; then sudo apt-get -qq install lua5.1 liblua5.1-dev; fi
|
||||
- if test "$SWIGLANG" = "octave"; then sudo apt-get -qq install octave3.2 octave3.2-headers; fi
|
||||
- if test "$SWIGLANG" = "php"; then sudo apt-get install php5-cli php5-dev; fi
|
||||
- if test "$SWIGLANG" = "python" -a "$PY3" -a -z "$VER"; then sudo apt-get install -qq python3-dev; fi
|
||||
- if test "$SWIGLANG" = "python" -a "$VER"; then sudo add-apt-repository -y ppa:fkrull/deadsnakes && sudo apt-get -qq update && sudo apt-get -qq install python${VER}-dev && export CONFIGOPTS="--with-python${PY3}=python${VER}"; fi
|
||||
- if test "$SWIGLANG" = "python" -a "$VER"; then sudo add-apt-repository -y ppa:fkrull/deadsnakes && sudo apt-get -qq update && sudo apt-get -qq install python${VER}-dev; fi
|
||||
- if test "$SWIGLANG" = "tcl"; then sudo apt-get -qq install tcl8.4-dev; fi
|
||||
script:
|
||||
- ./autogen.sh && ./configure $CONFIGOPTS
|
||||
- ./autogen.sh && ./configure
|
||||
- make -s $SWIGJOBS
|
||||
- ./swig -version && ./swig -pcreversion
|
||||
- if test -z "$SWIGLANG"; then make -s $SWIGJOBS check-ccache; fi
|
||||
|
|
@ -71,3 +46,4 @@ script:
|
|||
branches:
|
||||
only:
|
||||
- master
|
||||
- javascript
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ Active SWIG Developers:
|
|||
Joseph Wang (joequant@gmail.com) (R)
|
||||
Xavier Delacour (xavier.delacour@gmail.com) (Octave)
|
||||
David Nadlinger (code@klickverbot.at) (D)
|
||||
Oliver Buchtala (oliver.buchtala@gmail.com) (Javascript)
|
||||
Neha Narang (narangneha03@gmail.com) (Javascript)
|
||||
|
||||
Past SWIG developers and major contributors include:
|
||||
Dave Beazley (dave-swig@dabeaz.com) (SWIG core, Python, Tcl, Perl)
|
||||
|
|
|
|||
808
Doc/Manual/Javascript.html
Normal file
808
Doc/Manual/Javascript.html
Normal file
|
|
@ -0,0 +1,808 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" type="text/css" href="./style.css">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<H1>SWIG and Javascript</H1>
|
||||
<p>This chapter describes SWIG's support of Javascript. It does not cover SWIG basics, but only information that is specific to this module.</p>
|
||||
|
||||
<H2>Overview</H2>
|
||||
<p>Javascript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. Its arguably the most popular language for web development.
|
||||
Javascript has gone beyond being a browser-based scripting language and with <a href="http://nodejs.org">node.js</a>, it is also used as a backend development language.</p>
|
||||
<p>Native Javascript extensions can be used for applications that embed a web-browser view or that embed a Javascript engine (such as <em>node.js</em>). Extending a general purpose web-browser is not possible as this would be a severe security issue.</p>
|
||||
<p>SWIG Javascript currently supports <strong>JavascriptCore</strong>, the Javascript engine used by <code>Safari/Webkit</code>, and <strong>v8</strong>, which is used by <code>Chromium</code> and <code>node.js</code>.</p>
|
||||
<p><a href="http://www.webkit.org/">WebKit</a> is a modern browser implementation available as open-source which can be embedded into an application.
|
||||
With <a href="https://github.com/rogerwang/node-webkit">node-webkit</a> there is a platform which uses Google's <code>Chromium</code> as Web-Browser widget and <code>node.js</code> for javascript extensions.
|
||||
</p>
|
||||
|
||||
<H2>Preliminaries</H2>
|
||||
|
||||
<H3>Running SWIG</H3>
|
||||
|
||||
<p>Suppose that you defined a SWIG module such as the following:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
int gcd(int x, int y);
|
||||
extern double Foo;</pre>
|
||||
</div>
|
||||
<p>To build a Javascript module, run SWIG using the <code>-javascript</code> option and a desired target engine <code>-jsc</code>, <code>-v8</code>, or <code>-node</code>. The generator for <code>node</code> is essentially delegating to the <code>v8</code> generator and adds some necessary preprocessor definitions.</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ swig -javascript -jsc example.i</pre>
|
||||
</div>
|
||||
<p>If building a C++ extension, add the -c++ option:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ swig -c++ -javascript -jsc example.i</pre>
|
||||
</div>
|
||||
<p>This creates a C/C++ source file <code>example_wrap.c</code> or <code>example_wrap.cxx</code>. The generated C source file contains the low-level wrappers that need to be compiled and linked with the rest of your C/C++ application to create an extension module.</p>
|
||||
<p>The name of the wrapper file is derived from the name of the input file. For example, if the input file is <code>example.i</code>, the name of the wrapper file is <code>example_wrap.c</code>. To change this, you can use the -o option. The wrapped module will export one function which must be called to register the module with the Javascript interpreter. For example, if your module is named <code>example</code> the corresponding initializer for JavascriptCore would be</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
bool example_initialize(JSGlobalContextRef context, JSObjectRef *exports)</pre>
|
||||
</div>
|
||||
<p>and for v8:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
void example_initialize(v8::Handle<v8::Object> exports)</pre>
|
||||
</div>
|
||||
<p>
|
||||
<p><b>Note</b>: be aware that <code>v8</code> has a C++ API, and thus, the generated modules must be compiled as C++.</p>
|
||||
</p>
|
||||
|
||||
<H3>Running Tests and Examples</H3>
|
||||
<p>The configuration for tests and examples currently supports Linux and Mac only and not MinGW (Windows) yet.</p>
|
||||
<p>The default interpreter is <code>node.js</code> as it is available on all platforms and convenient to use.</p>
|
||||
<p>Running the examples with JavascriptCore requires <code>libjavascriptcoregtk-1.0</code> to be installed, e.g., under Ubuntu with</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ sudo apt-get install libjavascriptcoregtk-1.0-dev</pre>
|
||||
</div>
|
||||
<p>Running with <code>V8</code> requires <code>libv8</code>:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ sudo apt-get install libv8-dev</pre>
|
||||
</div>
|
||||
<p>Examples can be run using</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ make check-javascript-examples ENGINE=jsc</pre>
|
||||
</div>
|
||||
<p><code>ENGINE</code> can be <code>node</code>, <code>jsc</code>, or <code>v8</code>.</p>
|
||||
<p>The test-suite can be run using</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ make check-javascript-test-suite ENGINE=jsc</pre>
|
||||
</div>
|
||||
<p>Tests should run without any problems, i.e., have been tried out, on the following platforms/interpreters:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
- Ubuntu Precise 12.04 64bit
|
||||
- JavascriptCore (Webkit 1.8.3)
|
||||
- Node.js (0.10.26)
|
||||
- v8 (3.7.12)
|
||||
- Ubuntu Saucy 13.10 64bit
|
||||
- JavascriptCore (Webkit 1.10.2)
|
||||
- Node.js
|
||||
- v8 (3.14.5)
|
||||
- Mac OSX Mountain Lion 10.8
|
||||
- JavascriptCore (built-in)
|
||||
- Node.js
|
||||
- Windows 7 64bit (VS 2010)
|
||||
- Node.js</pre>
|
||||
</div>
|
||||
<p>
|
||||
|
||||
<H3>Future work</H3>
|
||||
<p>The Javascript module is not yet as mature as other modules and some things are still missing. As it makes use of SWIG's Unified Typemap Library (UTL), many typemaps are inherited. We could work on that if requested:</p>
|
||||
<ul>
|
||||
<li><p>More typemaps: compared to other modules there are only a few typemaps implemented. For instance a lot of the <code>std_*.i</code> typemaps are missing, such as <code>std_iostream</code>, for instance.</p></li>
|
||||
<li><p>Director support: this would allow to extend a C++ abstract base class in Javascript. A pragmatic intermediate step for the most important usecase would be to support Javascript callbacks as arguments.</p></li>
|
||||
</ul>
|
||||
|
||||
<H2>Integration</H2>
|
||||
<p>This chapter gives a short introduction how to use a native Javascript extension: as a <code>node.js</code> module, and as an extension for an embedded Webkit.</p>
|
||||
|
||||
<H3>Creating <code>node.js</code> Extensions</H3>
|
||||
<p>To install <code>node.js</code> you can download an installer from their <a href="https://launchpad.net/~chris-lea/+archive/node.js">web-site</a> for OSX and Windows. For Linux you can either build the source yourself and run <code>sudo checkinstall</code> or keep to the (probably stone-age) packaged version. For Ubuntu there is a <a href="https://launchpad.net/~chris-lea/+archive/node.js/">PPA</a> available.</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ sudo add-apt-repository ppa:chris-lea/node.js
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install nodejs</pre>
|
||||
</div>
|
||||
<p>As <code>v8</code> is written in C++ and comes as a C++ library it is crucial to compile your module using the same compiler flags as used for building v8. To make things easier, <code>node.js</code> provides a build tool called <code>node-gyp</code>.</p>
|
||||
<p>You have to install it using <code>npm</code>:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ sudo npm install -g node-gyp</pre>
|
||||
</div>
|
||||
<p><code>node-gyp</code> expects a configuration file named <code>binding.gyp</code> which is basically in JSON format and conforms to the same format that is used with Google's build-tool <code>gyp</code>.</p>
|
||||
<p><code>binding.gyp</code>:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}</pre>
|
||||
</div>
|
||||
<p>First create the wrapper using SWIG:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ swig -javascript -node -c++ example.cxx</pre>
|
||||
</div>
|
||||
<p>Then run <code>node-gyp</code></p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ node-gyp</pre>
|
||||
</div>
|
||||
<p>This will create a <code>build</code> folder containing the native module. To use the extension you need to 'require' it in your Javascript source file:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
require("./build/Release/example")</pre>
|
||||
</div>
|
||||
<p>A more detailed explanation is given in the <a href="#Javascript_examples">Examples</a> section.</p>
|
||||
|
||||
<H4>Troubleshooting</H4>
|
||||
<ul>
|
||||
<li><em>'module' object has no attribute 'script_main'</em></li>
|
||||
</ul>
|
||||
<p>This error happens when <code>gyp</code> is installed as a distribution package. It seems to be outdated. Removing it resolves the problem.</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ sudo apt-get remove gyp</pre>
|
||||
</div>
|
||||
|
||||
<H3>Embedded Webkit</H3>
|
||||
<p>Webkit is pre-installed on OSX and available as a library for GTK.</p>
|
||||
|
||||
<H4>OSX</H4>
|
||||
<p>There is general information about programming with WebKit on <a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/DisplayWebContent/DisplayWebContent.html">Apple Developer Documentation</a>. Details about <code>Cocoa</code> programming are not covered here.</p>
|
||||
<p>An integration of a native extension 'example' would look like this:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
#import "appDelegate.h"
|
||||
|
||||
extern bool example_initialize(JSGlobalContextRef context);
|
||||
|
||||
|
||||
@implementation ExampleAppDelegate
|
||||
|
||||
@synthesize webView;
|
||||
|
||||
|
||||
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
|
||||
|
||||
// Start a webview with the bundled index.html file
|
||||
NSString *path = [[NSBundle mainBundle] bundlePath];
|
||||
NSString *url = [NSString stringWithFormat: @"file://%@/Contents/Assets/index.html", path];
|
||||
|
||||
WebFrame *webframe = [webView mainFrame];
|
||||
JSGlobalContextRef context = [webframe globalContext];
|
||||
|
||||
example_initialize(context);
|
||||
|
||||
[ [webView mainFrame] loadRequest:
|
||||
[NSURLRequest requestWithURL: [NSURL URLWithString:url] ]
|
||||
];
|
||||
}
|
||||
|
||||
@end</pre>
|
||||
</div>
|
||||
|
||||
<H4>GTK</H4>
|
||||
<p>There is general information about programming GTK at <a href="https://developer.gnome.org/gtk2/">GTK documentation</a> and in the <a href="https://developer.gnome.org/gtk-tutorial">GTK tutorial</a>, and for Webkit there is a <a href="http://webkitgtk.org/reference/webkitgtk/stable/index.html">Webkit GTK+ API Reference</a>.</p>
|
||||
<p>An integration of a native extension 'example' would look like this:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
#include <gtk/gtk.h>
|
||||
#include <webkit/webkit.h>
|
||||
|
||||
extern bool example_initialize(JSGlobalContextRef context);
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// Initialize GTK+
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
...
|
||||
|
||||
// Create a browser instance
|
||||
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
|
||||
WebFrame *webframe = webkit_web_view_get_main_frame(webView);
|
||||
JSGlobalContextRef context = webkit_web_frame_get_global_context(webFrame);
|
||||
example_initialize(context);
|
||||
|
||||
...
|
||||
|
||||
// Load a web page into the browser instance
|
||||
webkit_web_view_load_uri(webView, "http://www.webkitgtk.org/");
|
||||
|
||||
...
|
||||
|
||||
// Run the main GTK+ event loop
|
||||
gtk_main();
|
||||
|
||||
return 0;
|
||||
}</pre>
|
||||
</div>
|
||||
|
||||
<H3>Creating Applications with <code>node-webkit</code></H3>
|
||||
<p>
|
||||
<p>To get started with <code>node-webkit</code> there is a very informative set of <a href="https://github.com/rogerwang/node-webkit/wiki">wiki pages</a>.</p>
|
||||
<p>Similar to <code>node.js</code>, <code>node-webkit</code> is started from command line within a <code>node.js</code> project directory.
|
||||
Native extensions are created in the very same way as for <code>node.js</code>, except that a customized <code>gyp</code> derivate has to be used: <a href="https://github.com/rogerwang/nw-gyp">nw-gyp</a>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
A simple example would have the following structure:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
- package.json
|
||||
- app.html
|
||||
- app.js
|
||||
- node_modules
|
||||
/ example
|
||||
... (as known from node.js)
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
The configuration file essentially conforms to <code>node.js</code> syntax.
|
||||
It has some extras to configure <code>node-webkit</code>. See the <a href="">Manifest</a> specification for more details.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<code>package.json</code>:
|
||||
</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
{
|
||||
"name": "example"
|
||||
"main": "app.html"
|
||||
"window": {
|
||||
"show": true,
|
||||
"width": 800,
|
||||
"height": 600
|
||||
}
|
||||
}</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
The <code>'main'</code> property of <code>package.json</code> specifies a web-page to be rendered in
|
||||
the main window.</p>
|
||||
|
||||
<p>
|
||||
<code>app.html</code>:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
<html>
|
||||
<head>
|
||||
<script src="app.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
The greatest common divisor of
|
||||
<span id="x"></span> and
|
||||
<span id="y"></span> is
|
||||
<span id="z"></span>.
|
||||
</div>
|
||||
</body>
|
||||
</html></pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
As known from <code>node.js</code> one can use <code>require</code> to load javascript modules.
|
||||
Additionally, <code>node-webkit</code> provides an API that allows to manipulate the window's menu,
|
||||
open new windows, and many more things.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<code>app.js</code>:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>window.onload = function() {
|
||||
var example = require("example");
|
||||
var x = 18;
|
||||
var y = 24;
|
||||
var z = example.gcd(x,y);
|
||||
document.querySelector('#x').innerHTML = x;
|
||||
document.querySelector('#y').innerHTML = y;
|
||||
document.querySelector('#z').innerHTML = z;
|
||||
};</pre>
|
||||
</div>
|
||||
|
||||
<H2><a name="Javascript_examples">Examples</H2>
|
||||
<p>Some basic examples are shown here in more detail.</p>
|
||||
|
||||
<H3>Simple</H3>
|
||||
<p>The common example <code>simple</code> looks like this:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
%}</pre>
|
||||
</div>
|
||||
<p>To make this available as a node extension a <code>binding.gyp</code> has to be created:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}</pre>
|
||||
</div>
|
||||
<p>Then <code>node-gyp</code> is used to build the extension:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ node-gyp configure build</pre>
|
||||
</div>
|
||||
<p>From a 'nodejs` application the extension would be used like this:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
// import the extension via require
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
// calling the global method
|
||||
var x = 42;
|
||||
var y = 105;
|
||||
var g = example.gcd(x,y);
|
||||
|
||||
// Accessing the globak variable
|
||||
var f = example.Foo;
|
||||
example.Foo = 3.1415926;</pre>
|
||||
</div>
|
||||
<p>First the module <code>example</code> is loaded from the previously built extension. Global methods and variables are available in the scope of the module.</p>
|
||||
<p>
|
||||
<p><b>Note</b>: ECMAScript 5, the currently implemented Javascript standard, does not have modules. <code>node.js</code> and other implementations provide this mechanism defined by the <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> group. For browsers this is provided by <a href="http://browserify.org">Browserify</a>, for instance.</p>
|
||||
</p>
|
||||
|
||||
<H3>Class</H3>
|
||||
<p>The common example <code>class</code> defines three classes, <code>Shape</code>, <code>Circle</code>, and <code>Square</code>:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};</pre>
|
||||
</div>
|
||||
<p><code>Circle</code> and <code>Square</code> inherit from <code>Shape</code>. <code>Shape</code> has a static variable <code>nshapes</code>, a function <code>move</code> that can't be overridden (non-virtual), and two abstract functions <code>area</code> and <code>perimeter</code> (pure virtual) that must be overridden by the sub-classes.</p>
|
||||
<p>A <code>nodejs</code> extension is built the same way as for the <code>simple</code> example.</p>
|
||||
<p>In Javascript it can be used as follows:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
// local aliases for convenience
|
||||
var Shape = example.Shape;
|
||||
var Circle = example.Circle;
|
||||
var Square = example.Square;
|
||||
|
||||
// creating new instances using the 'new' operator
|
||||
var c = new Circle(10);
|
||||
var s = new Square(10);
|
||||
|
||||
// accessing a static member
|
||||
Shape.nshapes;
|
||||
|
||||
// accessing member variables
|
||||
c.x = 20;
|
||||
c.y = 30;
|
||||
s.x = -10;
|
||||
s.y = 5;
|
||||
|
||||
// calling some methods
|
||||
c.area();
|
||||
c.perimeter();
|
||||
s.area();
|
||||
s.perimeter();
|
||||
|
||||
// instantiation of Shape is not permitted
|
||||
new Shape();</pre>
|
||||
</div>
|
||||
<p>Running these commands in an interactive node shell results in the following output:</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ node -i
|
||||
> var example = require("./build/Release/example");
|
||||
undefined
|
||||
> var Shape = example.Shape;
|
||||
undefined
|
||||
> var Circle = example.Circle;
|
||||
undefined
|
||||
> var Square = example.Square;
|
||||
undefined
|
||||
> var c = new Circle(10);
|
||||
undefined
|
||||
> var s = new Square(10);
|
||||
undefined
|
||||
> Shape.nshapes;
|
||||
2
|
||||
> c.x = 20;
|
||||
20
|
||||
> c.y = 30;
|
||||
30
|
||||
> s.x = -10;
|
||||
-10
|
||||
> s.y = 5;
|
||||
5
|
||||
> c.area();
|
||||
314.1592653589793
|
||||
> c.perimeter();
|
||||
62.83185307179586
|
||||
> s.area();
|
||||
100
|
||||
> s.perimeter();
|
||||
40
|
||||
> c.move(40, 40)
|
||||
undefined
|
||||
> c.x
|
||||
60
|
||||
> c.y
|
||||
70
|
||||
> new Shape()
|
||||
Error: Class Shape can not be instantiated
|
||||
at repl:1:2
|
||||
at REPLServer.self.eval (repl.js:110:21)
|
||||
at Interface.<anonymous> (repl.js:239:12)
|
||||
at Interface.EventEmitter.emit (events.js:95:17)
|
||||
at Interface._onLine (readline.js:202:10)
|
||||
at Interface._line (readline.js:531:8)
|
||||
at Interface._ttyWrite (readline.js:760:14)
|
||||
at ReadStream.onkeypress (readline.js:99:10)
|
||||
at ReadStream.EventEmitter.emit (events.js:98:17)
|
||||
at emitKey (readline.js:1095:12)</pre>
|
||||
</div>
|
||||
<p>
|
||||
<p><b>Note</b>: In ECMAScript 5 there is no concept for classes. Instead each function can be used as a constructor function which is executed by the 'new' operator. Furthermore, during construction the key property <code>prototype</code> of the constructor function is used to attach a prototype instance to the created object. A prototype is essentially an object itself that is the first-class delegate of a class used whenever the access to a property of an object fails. The very same prototype instance is shared among all instances of one type. Prototypal inheritance is explained in more detail on in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain">Inheritance and the prototype chain</a>, for instance.</p>
|
||||
</p>
|
||||
|
||||
<H2>Implementation</H2>
|
||||
<p>The Javascript Module implementation has taken a very different approach compared to other language modules in order to support different Javascript interpreters.</p>
|
||||
|
||||
<H3>Source Code</H3>
|
||||
<p>The Javascript module is implemented in <code>Source/Modules/javascript.cxx</code>. It dispatches the code generation to a <code>JSEmitter</code> instance, <code>V8Emitter</code> or <code>JSCEmitter</code>. Additionally there are some helpers: <code>Template</code>, for templated code generation, and <code>JSEmitterState</code>, which is used to manage state information during AST traversal. This rough map shall make it easier to find a way through this huge source file:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
// module wide defines
|
||||
|
||||
#define NAME "name"
|
||||
...
|
||||
|
||||
// ###############################
|
||||
// # Helper class declarations
|
||||
|
||||
class JSEmitterState { ... };
|
||||
|
||||
class Template { ... };
|
||||
|
||||
// ###############################
|
||||
// # JSEmitter declaration
|
||||
|
||||
class JSEmitter { ... };
|
||||
|
||||
// Emitter factory declarations
|
||||
|
||||
JSEmitter *swig_javascript_create_JSCEmitter();
|
||||
JSEmitter *swig_javascript_create_V8Emitter();
|
||||
|
||||
// ###############################
|
||||
// # Javascript module
|
||||
|
||||
// Javascript module declaration
|
||||
|
||||
class JAVASCRIPT:public Language { ... };
|
||||
|
||||
// Javascript module implementation
|
||||
|
||||
int JAVASCRIPT::functionWrapper(Node *n) { ... }
|
||||
...
|
||||
|
||||
// Module factory implementation
|
||||
|
||||
static Language *new_swig_javascript() { ... }
|
||||
|
||||
extern "C" Language *swig_javascript(void) { ... }
|
||||
|
||||
// ###############################
|
||||
// # JSEmitter base implementation
|
||||
|
||||
JSEmitter::JSEmitter() { ... }
|
||||
|
||||
Template JSEmitter::getTemplate(const String *name) { ... }
|
||||
...
|
||||
|
||||
// ###############################
|
||||
// # JSCEmitter
|
||||
|
||||
// JSCEmitter declaration
|
||||
|
||||
class JSCEmitter: public JSEmitter { ... };
|
||||
|
||||
// JSCEmitter implementation
|
||||
|
||||
JSCEmitter::JSCEmitter() { ... }
|
||||
|
||||
void JSCEmitter::marshalInputArgs(Node *n, ParmList *parms, Wrapper *wrapper, MarshallingMode mode, bool is_member, bool is_static) { ... }
|
||||
...
|
||||
|
||||
// JSCEmitter factory
|
||||
|
||||
JSEmitter *swig_javascript_create_JSCEmitter() { ... }
|
||||
|
||||
|
||||
// ###############################
|
||||
// # V8Emitter
|
||||
|
||||
// V8Emitter declaration
|
||||
|
||||
class V8Emitter: public JSEmitter { ... };
|
||||
|
||||
// V8Emitter implementation
|
||||
|
||||
V8Emitter::V8Emitter() { ... }
|
||||
|
||||
int V8Emitter::initialize(Node *n) { ... }
|
||||
|
||||
// V8Emitter factory
|
||||
|
||||
JSEmitter *swig_javascript_create_V8Emitter() { ... }
|
||||
|
||||
|
||||
// ###############################
|
||||
// # Helper implementation (JSEmitterState, Template)
|
||||
|
||||
JSEmitterState::JSEmitterState() { ... }
|
||||
...
|
||||
|
||||
Template::Template(const String *code_) { ... }
|
||||
...</pre>
|
||||
</div>
|
||||
|
||||
<H3>Code Templates</H3>
|
||||
<p>All generated code is created on the basis of code templates. The templates for <em>JavascriptCore</em> can be found in <code>Lib/javascript/jsc/javascriptcode.swg</code>, for <em>v8</em> in <code>Lib/javascript/v8/javascriptcode.swg</code>.</p>
|
||||
<p>To track the originating code template for generated code you can run</p>
|
||||
<div class="shell">
|
||||
<pre>
|
||||
$ swig -javascript -jsc -debug-codetemplates</pre>
|
||||
</div>
|
||||
<p>which wraps generated code with a descriptive comment</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
/* begin fragment("template_name") */
|
||||
|
||||
...generated code ...
|
||||
|
||||
/* end fragment("template_name") */</pre>
|
||||
</div>
|
||||
<p>The Template class is used like this:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
Template t_register = getTemplate("jsv8_register_static_variable");
|
||||
t_register.replace("$jsparent", state.clazz(NAME_MANGLED))
|
||||
.replace("$jsname", state.variable(NAME))
|
||||
.replace("$jsgetter", state.variable(GETTER))
|
||||
.replace("$jssetter", state.variable(SETTER))
|
||||
.trim().
|
||||
print(f_init_static_wrappers);</pre>
|
||||
</div>
|
||||
<p>A code template is registered with the <em>JSEmitter</em> via <code>fragment(name, "template")</code>, e.g.,</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
%fragment ("jsc_variable_declaration", "templates")
|
||||
%{
|
||||
{"$jsname", $jsgetter, $jssetter, kJSPropertyAttributeNone},
|
||||
%}</pre>
|
||||
</div>
|
||||
<p><code>Template</code> creates a copy of that string and <code>Template::replace</code> uses Swig's <code>Replaceall</code> to replace variables in the template. <code>Template::trim</code> can be used to eliminate leading and trailing whitespaces. <code>Template::print</code> is used to write the final template string to a Swig <code>DOH</code> (based on <code>Printv</code>). All methods allow chaining.</p>
|
||||
|
||||
<H3>Emitter</H3>
|
||||
<p>The Javascript module delegates code generation to a <code>JSEmitter</code> instance. The following extract shows the essential interface:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
class JSEmitter {
|
||||
...
|
||||
|
||||
/**
|
||||
* Opens output files and temporary output DOHs.
|
||||
*/
|
||||
virtual int initialize(Node *n);
|
||||
|
||||
/**
|
||||
* Writes all collected code into the output file(s).
|
||||
*/
|
||||
virtual int dump(Node *n) = 0;
|
||||
|
||||
/**
|
||||
* Cleans up all open output DOHs.
|
||||
*/
|
||||
virtual int close() = 0;
|
||||
|
||||
...
|
||||
|
||||
/**
|
||||
* Invoked at the beginning of the classHandler.
|
||||
*/
|
||||
virtual int enterClass(Node *);
|
||||
|
||||
/**
|
||||
* Invoked at the end of the classHandler.
|
||||
*/
|
||||
virtual int exitClass(Node *) {
|
||||
return SWIG_OK;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked at the beginning of the variableHandler.
|
||||
*/
|
||||
virtual int enterVariable(Node *);
|
||||
|
||||
/**
|
||||
* Invoked at the end of the variableHandler.
|
||||
*/
|
||||
virtual int exitVariable(Node *) {
|
||||
return SWIG_OK;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked at the beginning of the functionHandler.
|
||||
*/
|
||||
virtual int enterFunction(Node *);
|
||||
|
||||
/**
|
||||
* Invoked at the end of the functionHandler.
|
||||
*/
|
||||
virtual int exitFunction(Node *) {
|
||||
return SWIG_OK;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked by functionWrapper callback after call to Language::functionWrapper.
|
||||
*/
|
||||
virtual int emitWrapperFunction(Node *n);
|
||||
|
||||
/**
|
||||
* Invoked from constantWrapper after call to Language::constantWrapper.
|
||||
**/
|
||||
virtual int emitConstant(Node *n);
|
||||
|
||||
/**
|
||||
* Registers a given code snippet for a given key name.
|
||||
*
|
||||
* This method is called by the fragmentDirective handler
|
||||
* of the JAVASCRIPT language module.
|
||||
**/
|
||||
int registerTemplate(const String *name, const String *code);
|
||||
|
||||
/**
|
||||
* Retrieve the code template registered for a given name.
|
||||
*/
|
||||
Template getTemplate(const String *name);
|
||||
|
||||
State &getState();
|
||||
|
||||
...
|
||||
|
||||
}</pre>
|
||||
</div>
|
||||
<p>The module calls <code>initialize</code>, <code>dump</code>, and <code>close</code> from within the <code>top</code> method:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
int JAVASCRIPT::top(Node *n) {
|
||||
emitter->initialize(n);
|
||||
|
||||
Language::top(n);
|
||||
|
||||
emitter->dump(n);
|
||||
emitter->close();
|
||||
|
||||
return SWIG_OK;
|
||||
}</pre>
|
||||
</div>
|
||||
<p>The methods <code>enterClass</code> and <code>exitClass</code> are called from within the <code>classHandler</code> method:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
int JAVASCRIPT::classHandler(Node *n) {
|
||||
|
||||
emitter->enterClass(n);
|
||||
Language::classHandler(n);
|
||||
emitter->exitClass(n);
|
||||
|
||||
return SWIG_OK;
|
||||
}</pre>
|
||||
</div>
|
||||
<p>In <code>enterClass</code> the emitter stores state information that is necessary when processing class members. In <code>exitClass</code> the wrapper code for the whole class is generated.</p>
|
||||
|
||||
<H3>Emitter states</H3>
|
||||
<p>For storing information during the AST traversal the emitter provides a <code>JSEmitterState</code> with different slots to store data representing the scopes global, class, function, and variable.</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
class JSEmitterState {
|
||||
|
||||
public:
|
||||
|
||||
JSEmitterState();
|
||||
|
||||
~JSEmitterState();
|
||||
|
||||
DOH *global();
|
||||
|
||||
DOH *global(const char* key, DOH *initial = 0);
|
||||
|
||||
DOH *clazz(bool reset = false);
|
||||
|
||||
DOH *clazz(const char* key, DOH *initial = 0);
|
||||
|
||||
DOH *function(bool reset = false);
|
||||
|
||||
DOH *function(const char* key, DOH *initial = 0);
|
||||
|
||||
DOH *variable(bool reset = false);
|
||||
|
||||
DOH *variable(const char* key, DOH *initial = 0);
|
||||
|
||||
static int IsSet(DOH *val);
|
||||
|
||||
...
|
||||
};</pre>
|
||||
</div>
|
||||
<p>When entering a scope, such as in <code>enterClass</code>, the corresponding state is reset and new data is stored:</p>
|
||||
<div class="code">
|
||||
<pre>
|
||||
state.clazz(RESET);
|
||||
state.clazz(NAME, Getattr(n, "sym:name"));</pre>
|
||||
</div>
|
||||
<p>State information can be retrieved using <code>state.clazz(NAME)</code> or with <code>Getattr</code> on <code>state.clazz()</code> which actually returns a <code>Hash</code> instance.</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -115,6 +115,9 @@ SWIGCLISP Defined when using CLISP
|
|||
SWIGCSHARP Defined when using C#
|
||||
SWIGGUILE Defined when using Guile
|
||||
SWIGJAVA Defined when using Java
|
||||
SWIGJAVASCRIPT Defined when using Javascript
|
||||
SWIG_JAVASCRIPT_JSC Defined when using Javascript for JavascriptCore
|
||||
SWIG_JAVASCRIPT_V8 Defined when using Javascript for v8 or node.js
|
||||
SWIGLUA Defined when using Lua
|
||||
SWIGMODULA3 Defined when using Modula-3
|
||||
SWIGMZSCHEME Defined when using Mzscheme
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ Last update : SWIG-3.0.1 (in progress)
|
|||
<li><a href="Go.html#Go">Go support</a></li>
|
||||
<li><a href="Guile.html#Guile">Guile support</a></li>
|
||||
<li><a href="Java.html#Java">Java support</a></li>
|
||||
<li><a href="Javascript.html#Java">Javascript support</a></li>
|
||||
<li><a href="Lisp.html#Lisp">Common Lisp support</a></li>
|
||||
<li><a href="Lua.html#Lua">Lua support</a></li>
|
||||
<li><a href="Modula3.html#Modula3">Modula3 support</a></li>
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ D.html
|
|||
Go.html
|
||||
Guile.html
|
||||
Java.html
|
||||
Javascript.html
|
||||
Lisp.html
|
||||
Lua.html
|
||||
Modula3.html
|
||||
|
|
|
|||
|
|
@ -589,6 +589,117 @@ java_clean:
|
|||
rm -f core @EXTRA_CLEAN@
|
||||
rm -f *.@OBJEXT@ *@JAVASO@
|
||||
|
||||
##################################################################
|
||||
##### JAVASCRIPT ######
|
||||
##################################################################
|
||||
|
||||
# Note: These targets are also from within Makefiles in the Example directories.
|
||||
# There is a common makefile, 'Examples/javascript/js_example.mk' to simplify
|
||||
# create a configuration for a new example.
|
||||
|
||||
ROOT_DIR = @ROOT_DIR@
|
||||
JSINCLUDES = @JSCOREINC@ @JSV8INC@
|
||||
JSDYNAMICLINKING = @JSCOREDYNAMICLINKING@ @JSV8DYNAMICLINKING@
|
||||
JSSO =@JSSO@
|
||||
JSLDSHARED = @JSLDSHARED@
|
||||
JSCXXSHARED = @JSCXXSHARED@
|
||||
NODEJS = @NODEJS@
|
||||
NODEGYP = @NODEGYP@
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Creating and building Javascript wrappers
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
javascript_wrapper:
|
||||
$(SWIG) -javascript $(SWIGOPT) -o $(INTERFACEDIR)$(TARGET)_wrap.c $(INTERFACEPATH)
|
||||
|
||||
javascript_wrapper_cpp: $(SRCS)
|
||||
$(SWIG) -javascript -c++ $(SWIGOPT) -o $(INTERFACEDIR)$(TARGET)_wrap.cxx $(INTERFACEPATH)
|
||||
|
||||
javascript_build: $(SRCS)
|
||||
$(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES) $(JSINCLUDES)
|
||||
$(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(JSDYNAMICLINKING) $(LIBS) -o $(LIBPREFIX)$(TARGET)$(SO)
|
||||
|
||||
javascript_build_cpp: $(SRCS)
|
||||
ifeq (node,$(JSENGINE))
|
||||
$(NODEGYP) --loglevel=silent configure build 1>>/dev/null
|
||||
else
|
||||
$(CXX) -c $(CCSHARED) $(CFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES) $(JSINCLUDES)
|
||||
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(JSDYNAMICLINKING) $(LIBS) $(CPP_DLLIBS) -o $(LIBPREFIX)$(TARGET)$(SO)
|
||||
|
||||
endif
|
||||
|
||||
# These targets are used by the test-suite:
|
||||
|
||||
javascript: $(SRCS) javascript_custom_interpreter
|
||||
$(SWIG) -javascript $(SWIGOPT) $(INTERFACEPATH)
|
||||
ifeq (jsc, $(ENGINE))
|
||||
$(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES) $(JSINCLUDES)
|
||||
$(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(JSDYNAMICLINKING) $(LIBS) -o $(LIBPREFIX)$(TARGET)$(SO)
|
||||
else # (v8 | node) # v8 and node must be compiled as c++
|
||||
$(CXX) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(CXXSRCS) $(INCLUDES) $(JSINCLUDES)
|
||||
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(JSDYNAMICLINKING) $(LIBS) $(CPP_DLLIBS) -o $(LIBPREFIX)$(TARGET)$(SO)
|
||||
endif
|
||||
|
||||
javascript_cpp: $(SRCS) javascript_custom_interpreter
|
||||
$(SWIG) -javascript -c++ $(SWIGOPT) $(INTERFACEPATH)
|
||||
$(CXX) -c $(CCSHARED) $(CFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES) $(JSINCLUDES)
|
||||
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(JSDYNAMICLINKING) $(LIBS) $(CPP_DLLIBS) -o $(LIBPREFIX)$(TARGET)$(SO)
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Running a Javascript example
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
javascript_custom_interpreter:
|
||||
(cd $(ROOT_DIR)/Tools/javascript && $(MAKE) JSENGINE='$(JSENGINE)')
|
||||
|
||||
ifeq (node,$(JSENGINE))
|
||||
javascript_run:
|
||||
$(RUNTOOL) $(NODEJS) $(RUNME).js $(RUNPIPE)
|
||||
else
|
||||
javascript_run: javascript_custom_interpreter
|
||||
$(RUNTOOL) $(ROOT_DIR)/Tools/javascript/javascript -$(JSENGINE) -L $(TARGET) $(RUNME).js $(RUNPIPE)
|
||||
endif
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Version display
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
javascript_version:
|
||||
ifeq (, $(ENGINE))
|
||||
@if [ "$(NODEJS)" != "" ]; then \
|
||||
echo "Node.js: `($(NODEJS) --version)`"; \
|
||||
echo "node-gyp: `($(NODEGYP) --version)`"; \
|
||||
else \
|
||||
echo "Version depends on the interpreter"; \
|
||||
fi
|
||||
endif
|
||||
ifeq (node, $(ENGINE))
|
||||
echo "Node.js: `($(NODEJS) --version)`"
|
||||
echo "node-gyp: `($(NODEGYP) --version)`"
|
||||
endif
|
||||
ifeq (jsc, $(ENGINE))
|
||||
@if [ "@JSCOREVERSION@" != "" ]; then \
|
||||
echo "@JSCOREVERSION@"; \
|
||||
else \
|
||||
echo "Unknown JavascriptCore version."; \
|
||||
fi
|
||||
endif
|
||||
ifeq (v8, $(ENGINE))
|
||||
echo "Unknown v8 version."
|
||||
endif
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Cleaning the Javascript examples
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
javascript_clean:
|
||||
rm -rf build
|
||||
rm -f *_wrap* $(RUNME)
|
||||
rm -f core @EXTRA_CLEAN@
|
||||
rm -f *.@OBJEXT@ *@JSSO@ *.$(SO)
|
||||
(cd $(ROOT_DIR)/Tools/javascript && $(MAKE) -s clean)
|
||||
|
||||
##################################################################
|
||||
##### ANDROID ######
|
||||
##################################################################
|
||||
|
|
|
|||
13
Examples/javascript/check.list
Normal file
13
Examples/javascript/check.list
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class
|
||||
constant
|
||||
enum
|
||||
exception
|
||||
functor
|
||||
nspace
|
||||
operator
|
||||
overload
|
||||
pointer
|
||||
#reference
|
||||
simple
|
||||
template
|
||||
variables
|
||||
3
Examples/javascript/class/Makefile
Executable file
3
Examples/javascript/class/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/class/binding.gyp
Normal file
8
Examples/javascript/class/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
28
Examples/javascript/class/example.cxx
Executable file
28
Examples/javascript/class/example.cxx
Executable file
|
|
@ -0,0 +1,28 @@
|
|||
/* File : example.c */
|
||||
#include <iostream>
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
34
Examples/javascript/class/example.h
Executable file
34
Examples/javascript/class/example.h
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
10
Examples/javascript/class/example.i
Executable file
10
Examples/javascript/class/example.i
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
1
Examples/javascript/class/example.js
Normal file
1
Examples/javascript/class/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
46
Examples/javascript/class/runme.js
Executable file
46
Examples/javascript/class/runme.js
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
var example = require("./example");
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
console.log("Creating some objects:");
|
||||
c = new example.Circle(10);
|
||||
console.log("Created circle " + c);
|
||||
s = new example.Square(10);
|
||||
console.log("Created square " + s);
|
||||
|
||||
// ----- Access a static member -----
|
||||
console.log("\nA total of " + example.Shape.nshapes + " shapes were created"); // access static member as properties of the class object
|
||||
|
||||
// ----- Member data access -----
|
||||
// Set the location of the object.
|
||||
// Note: methods in the base class Shape are used since
|
||||
// x and y are defined there.
|
||||
|
||||
c.x = 20;
|
||||
c.y = 30;
|
||||
s.x = -10;
|
||||
s.y = 5;
|
||||
|
||||
console.log("\nHere is their new position:");
|
||||
console.log("Circle = (" + c.x + "," + c.y + ")");
|
||||
console.log("Square = (" + s.x + "," + s.y + ")");
|
||||
|
||||
// ----- Call some methods -----
|
||||
console.log("\nHere are some properties of the shapes:");
|
||||
console.log("Circle:");
|
||||
console.log("area = " + c.area() + "");
|
||||
console.log("perimeter = " + c.perimeter() + "");
|
||||
console.log("\n");
|
||||
console.log("Square:");
|
||||
console.log("area = " + s.area() + "");
|
||||
console.log("perimeter = " + s.perimeter() + "");
|
||||
|
||||
// ----- Delete everything -----
|
||||
console.log("\nGuess I'll clean up now");
|
||||
// Note: this invokes the virtual destructor
|
||||
delete c;
|
||||
delete s;
|
||||
|
||||
console.log(example.Shape.nshapes + " shapes remain");
|
||||
|
||||
console.log("Goodbye");
|
||||
3
Examples/javascript/constant/Makefile
Executable file
3
Examples/javascript/constant/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS =
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/constant/binding.gyp
Normal file
8
Examples/javascript/constant/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
8
Examples/javascript/constant/example.h
Normal file
8
Examples/javascript/constant/example.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
#define CCONST 'x'
|
||||
#define CCONST2 '\n'
|
||||
#define SCONST "Hello World"
|
||||
#define SCONST2 "\"Hello World\""
|
||||
#define EXTERN extern
|
||||
#define FOO (ICONST + BAR)
|
||||
24
Examples/javascript/constant/example.i
Executable file
24
Examples/javascript/constant/example.i
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* A few preprocessor macros */
|
||||
|
||||
#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
#define CCONST 'x'
|
||||
#define CCONST2 '\n'
|
||||
#define SCONST "Hello World"
|
||||
#define SCONST2 "\"Hello World\""
|
||||
|
||||
/* This should work just fine */
|
||||
#define EXPR ICONST + 3*(FCONST)
|
||||
|
||||
/* This shouldn't do anything */
|
||||
#define EXTERN extern
|
||||
|
||||
/* Neither should this (BAR isn't defined) */
|
||||
#define FOO (ICONST + BAR)
|
||||
|
||||
/* The following directives also produce constants */
|
||||
%constant int iconst = 37;
|
||||
%constant double fconst = 3.14;
|
||||
1
Examples/javascript/constant/example.js
Normal file
1
Examples/javascript/constant/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
14
Examples/javascript/constant/runme.js
Executable file
14
Examples/javascript/constant/runme.js
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
var example = require("./example");
|
||||
|
||||
console.log("ICONST = " + example.ICONST + " (should be 42)");
|
||||
console.log("FCONST = " + example.FCONST + " (should be 2.1828)");
|
||||
console.log("CCONST = " + example.CCONST + " (should be 'x')");
|
||||
console.log("CCONST2 = " + example.CCONST2 + " (this should be on a new line)");
|
||||
console.log("SCONST = " + example.SCONST + " (should be 'Hello World')");
|
||||
console.log("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')");
|
||||
console.log("EXPR = " + example.EXPR + " (should be 48.5484)");
|
||||
console.log("iconst = " + example.iconst + " (should be 37)");
|
||||
console.log("fconst = " + example.fconst + " (should be 3.14)");
|
||||
|
||||
console.log("EXTERN = " + example.EXTERN + " (should be undefined)");
|
||||
console.log("FOO = " + example.FOO + " (should be undefined)");
|
||||
3
Examples/javascript/enum/Makefile
Executable file
3
Examples/javascript/enum/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/enum/binding.gyp
Normal file
8
Examples/javascript/enum/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
37
Examples/javascript/enum/example.cxx
Executable file
37
Examples/javascript/enum/example.cxx
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void Foo::enum_test(speed s) {
|
||||
if (s == IMPULSE) {
|
||||
printf("IMPULSE speed\n");
|
||||
} else if (s == WARP) {
|
||||
printf("WARP speed\n");
|
||||
} else if (s == LUDICROUS) {
|
||||
printf("LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("Unknown speed\n");
|
||||
}
|
||||
}
|
||||
|
||||
void enum_test(color c, Foo::speed s) {
|
||||
if (c == RED) {
|
||||
printf("color = RED, ");
|
||||
} else if (c == BLUE) {
|
||||
printf("color = BLUE, ");
|
||||
} else if (c == GREEN) {
|
||||
printf("color = GREEN, ");
|
||||
} else {
|
||||
printf("color = Unknown color!, ");
|
||||
}
|
||||
if (s == Foo::IMPULSE) {
|
||||
printf("speed = IMPULSE speed\n");
|
||||
} else if (s == Foo::WARP) {
|
||||
printf("speed = WARP speed\n");
|
||||
} else if (s == Foo::LUDICROUS) {
|
||||
printf("speed = LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("speed = Unknown speed!\n");
|
||||
}
|
||||
}
|
||||
13
Examples/javascript/enum/example.h
Executable file
13
Examples/javascript/enum/example.h
Executable file
|
|
@ -0,0 +1,13 @@
|
|||
/* File : example.h */
|
||||
|
||||
enum color { RED, BLUE, GREEN };
|
||||
|
||||
class Foo {
|
||||
public:
|
||||
Foo() { }
|
||||
enum speed { IMPULSE=10, WARP=20, LUDICROUS=30 };
|
||||
void enum_test(speed s);
|
||||
};
|
||||
|
||||
void enum_test(color c, Foo::speed s);
|
||||
|
||||
11
Examples/javascript/enum/example.i
Executable file
11
Examples/javascript/enum/example.i
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%include "example.h"
|
||||
|
||||
1
Examples/javascript/enum/example.js
Normal file
1
Examples/javascript/enum/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
34
Examples/javascript/enum/runme.js
Executable file
34
Examples/javascript/enum/runme.js
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
var example = require("./example");
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
// Print out the value of some enums
|
||||
console.log("*** color ***");
|
||||
console.log(" RED =" + example.RED);
|
||||
console.log(" BLUE =" + example.BLUE);
|
||||
console.log(" GREEN =" + example.GREEN);
|
||||
|
||||
console.log("\n*** Foo::speed ***");
|
||||
console.log(" Foo_IMPULSE =" + example.Foo.IMPULSE);
|
||||
console.log(" Foo_WARP =" + example.Foo.WARP);
|
||||
console.log(" Foo_LUDICROUS =" + example.Foo.LUDICROUS);
|
||||
|
||||
console.log("\nTesting use of enums with functions\n");
|
||||
|
||||
example.enum_test(example.RED, example.Foo.IMPULSE);
|
||||
example.enum_test(example.BLUE, example.Foo.WARP);
|
||||
example.enum_test(example.GREEN, example.Foo.LUDICROUS);
|
||||
example.enum_test(1234,5678);
|
||||
|
||||
console.log("\nTesting use of enum with class method");
|
||||
f = new example.Foo();
|
||||
|
||||
f.enum_test(example.Foo.IMPULSE);
|
||||
f.enum_test(example.Foo.WARP);
|
||||
f.enum_test(example.Foo.LUDICROUS);
|
||||
|
||||
// enum value BLUE of enum color is accessed as property of cconst
|
||||
console.log("example.BLUE= " + example.BLUE);
|
||||
|
||||
// enum value LUDICROUS of enum Foo::speed is accessed as as property of cconst
|
||||
console.log("example.speed.LUDICROUS= " + example.Foo.LUDICROUS);
|
||||
27
Examples/javascript/example.mk
Normal file
27
Examples/javascript/example.mk
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Note: as a convention an example must be in a child directory of this.
|
||||
# These paths are relative to such an example directory
|
||||
|
||||
ifneq (, $(ENGINE))
|
||||
JSENGINE=$(ENGINE)
|
||||
else
|
||||
JSENGINE=node
|
||||
endif
|
||||
|
||||
EXAMPLES_TOP=../..
|
||||
SWIG_TOP=../../..
|
||||
SWIG = $(SWIG_TOP)/preinst-swig
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT=-$(JSENGINE)
|
||||
|
||||
check: build
|
||||
$(MAKE) -f $(EXAMPLES_TOP)/Makefile JSENGINE='$(JSENGINE)' TARGET='$(TARGET)' javascript_run
|
||||
|
||||
build:
|
||||
$(MAKE) -f $(EXAMPLES_TOP)/Makefile CXXSRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_wrapper_cpp
|
||||
$(MAKE) -f $(EXAMPLES_TOP)/Makefile CXXSRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' JSENGINE='$(JSENGINE)' javascript_build_cpp
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(EXAMPLES_TOP)/Makefile javascript_clean
|
||||
3
Examples/javascript/exception/Makefile
Executable file
3
Examples/javascript/exception/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
29
Examples/javascript/exception/binding.gyp
Normal file
29
Examples/javascript/exception/binding.gyp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ],
|
||||
'defines': [
|
||||
'BUILDING_NODE_EXTENSION=1',
|
||||
],
|
||||
'conditions': [
|
||||
['OS=="mac"',
|
||||
{
|
||||
'xcode_settings': {
|
||||
'GCC_ENABLE_CPP_RTTI': 'YES',
|
||||
'GCC_ENABLE_CPP_EXCEPTIONS' : 'YES'
|
||||
}
|
||||
}
|
||||
],
|
||||
['OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"',
|
||||
{
|
||||
'cflags': [ "-Wno-unused-variable", "-Wno-unused-but-set-variable", "-Wno-unused-but-set-parameter"],
|
||||
'cflags_cc': [ "-Wno-unused-variable", "-Wno-unused-but-set-variable", "-Wno-unused-but-set-parameter"],
|
||||
'cflags!': [ '-fno-exceptions' ],
|
||||
'cflags_cc!': [ '-fno-exceptions', '-fno-rtti' ]
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
1
Examples/javascript/exception/example.cxx
Normal file
1
Examples/javascript/exception/example.cxx
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
53
Examples/javascript/exception/example.h
Normal file
53
Examples/javascript/exception/example.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <string.h>
|
||||
#ifndef SWIG
|
||||
struct A {
|
||||
};
|
||||
#endif
|
||||
|
||||
class Exc {
|
||||
public:
|
||||
Exc(int c, const char *m) {
|
||||
code = c;
|
||||
strncpy(msg,m,256);
|
||||
}
|
||||
int code;
|
||||
char msg[256];
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
class Test {
|
||||
public:
|
||||
int simple() throw(int) {
|
||||
throw(37);
|
||||
return 1;
|
||||
}
|
||||
int message() throw(const char *) {
|
||||
throw("I died.");
|
||||
return 1;
|
||||
}
|
||||
int hosed() throw(Exc) {
|
||||
throw(Exc(42,"Hosed"));
|
||||
return 1;
|
||||
}
|
||||
int unknown() throw(A*) {
|
||||
static A a;
|
||||
throw &a;
|
||||
return 1;
|
||||
}
|
||||
int multi(int x) throw(int, const char *, Exc) {
|
||||
if (x == 1) throw(37);
|
||||
if (x == 2) throw("Bleah!");
|
||||
if (x == 3) throw(Exc(42,"No-go-diggy-die"));
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
12
Examples/javascript/exception/example.i
Normal file
12
Examples/javascript/exception/example.i
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "std_string.i"
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
1
Examples/javascript/exception/example.js
Normal file
1
Examples/javascript/exception/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
64
Examples/javascript/exception/runme.js
Normal file
64
Examples/javascript/exception/runme.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
var example = require("./example");
|
||||
|
||||
console.log("Trying to catch some exceptions.");
|
||||
t = new example.Test();
|
||||
try{
|
||||
t.unknown();
|
||||
throw -1;
|
||||
} catch(error)
|
||||
{
|
||||
if(error == -1) {
|
||||
console.log("t.unknown() didn't throw");
|
||||
} else {
|
||||
console.log("successfully catched throw in Test::unknown().");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.simple();
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
console.log("t.simple() did not throw");
|
||||
} else {
|
||||
console.log("successfully catched throw in Test::simple().");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.message();
|
||||
throw -1;
|
||||
} catch(error){
|
||||
if(error == -1) {
|
||||
console.log("t.message() did not throw");
|
||||
} else {
|
||||
console.log("successfully catched throw in Test::message().");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.hosed();
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
console.log("t.hosed() did not throw");
|
||||
} else {
|
||||
console.log("successfully catched throw in Test::hosed().");
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=1; i<4; i++) {
|
||||
try{
|
||||
t.multi(i);
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
console.log("t.multi(" + i + ") did not throw");
|
||||
} else {
|
||||
console.log("successfully catched throw in Test::multi().");
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Examples/javascript/functor/Makefile
Executable file
3
Examples/javascript/functor/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/functor/binding.gyp
Normal file
8
Examples/javascript/functor/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
0
Examples/javascript/functor/example.cxx
Normal file
0
Examples/javascript/functor/example.cxx
Normal file
25
Examples/javascript/functor/example.i
Normal file
25
Examples/javascript/functor/example.i
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
|
||||
%inline %{
|
||||
// From B. Strousjoup, "The C++ Programming Language, Third Edition", p. 514
|
||||
template<class T> class Sum {
|
||||
T res;
|
||||
public:
|
||||
Sum(T i = 0) : res(i) { }
|
||||
void operator() (T x) { res += x; }
|
||||
T result() const { return res; }
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
%rename(call) *::operator(); // the fn call operator
|
||||
|
||||
// Instantiate a few versions
|
||||
%template(intSum) Sum<int>;
|
||||
%template(doubleSum) Sum<double>;
|
||||
|
||||
|
||||
|
||||
|
||||
1
Examples/javascript/functor/example.js
Normal file
1
Examples/javascript/functor/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
15
Examples/javascript/functor/runme.js
Normal file
15
Examples/javascript/functor/runme.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
var example = require("./example");
|
||||
|
||||
a = new example.intSum(0);
|
||||
b = new example.doubleSum(100.0);
|
||||
|
||||
// Use the objects. They should be callable just like a normal
|
||||
// javascript function.
|
||||
|
||||
for (i=1;i<=100;i++)
|
||||
a.call(i); // Note: function call
|
||||
b.call(Math.sqrt(i)); // Note: function call
|
||||
|
||||
console.log(a.result());
|
||||
console.log(b.result());
|
||||
|
||||
3
Examples/javascript/nspace/Makefile
Executable file
3
Examples/javascript/nspace/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS =
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/nspace/binding.gyp
Normal file
8
Examples/javascript/nspace/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
23
Examples/javascript/nspace/example.h
Normal file
23
Examples/javascript/nspace/example.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef _example_guardian_
|
||||
#define _example_guardian_
|
||||
|
||||
int module_function() { return 7; }
|
||||
int module_variable = 9;
|
||||
|
||||
namespace MyWorld {
|
||||
class World {
|
||||
public:
|
||||
World() : world_max_count(9) {}
|
||||
int create_world() { return 17; }
|
||||
const int world_max_count; // = 9
|
||||
};
|
||||
namespace Nested {
|
||||
class Dweller {
|
||||
public:
|
||||
enum Gender { MALE = 0, FEMALE = 1 };
|
||||
static int count() { return 19; }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
10
Examples/javascript/nspace/example.i
Normal file
10
Examples/javascript/nspace/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%nspace MyWorld::Nested::Dweller;
|
||||
%nspace MyWorld::World;
|
||||
|
||||
%include "example.h"
|
||||
1
Examples/javascript/nspace/example.js
Normal file
1
Examples/javascript/nspace/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
50
Examples/javascript/nspace/runme.js
Normal file
50
Examples/javascript/nspace/runme.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// File: runme.js
|
||||
|
||||
// This file illustrates class C++ interface generated
|
||||
// by SWIG.
|
||||
|
||||
var example = require("./example");
|
||||
|
||||
// Calling a module function ( aka global function )
|
||||
if (example.module_function() !== 7) {
|
||||
throw new Error("example.module_function() should equal 7");
|
||||
}
|
||||
console.log("example.module_function(): " + example.module_function());
|
||||
|
||||
// Accessing a module (aka global) variable
|
||||
if (example.module_variable !== 9) {
|
||||
throw new Error("example.module_variable should equal 9");
|
||||
}
|
||||
console.log("example.module_variable: " + example.module_variable);
|
||||
|
||||
// Creating an instance of the class
|
||||
var w1 = new example.MyWorld.World();
|
||||
console.log("Creating class instance: w1 = new example.MyWorld.World(): " + w1);
|
||||
|
||||
// Accessing class members
|
||||
if (w1.create_world() !== 17) {
|
||||
throw new Error("w1.create_world() should equal 17");
|
||||
}
|
||||
console.log("w1.create_world() = " + w1.create_world());
|
||||
|
||||
if (w1.world_max_count !== 9) {
|
||||
throw new Error("w1.world_max_count should equal 9");
|
||||
}
|
||||
console.log("w1.world_max_count = " + w1.world_max_count);
|
||||
|
||||
// Accessing enums from class within namespace
|
||||
if (example.MyWorld.Nested.Dweller.MALE !== 0) {
|
||||
throw new Error("example.MyWorld.Nested.Dweller.MALE should equal 0");
|
||||
}
|
||||
console.log("Accessing enums: ex.MyWorld.Nested.Dweller.MALE = " + example.MyWorld.Nested.Dweller.MALE);
|
||||
|
||||
if (example.MyWorld.Nested.Dweller.FEMALE !== 1) {
|
||||
throw new Error("example.MyWorld.Nested.Dweller.FEMALE should equal 1");
|
||||
}
|
||||
console.log("Accessing enums: ex.MyWorld.Nested.Dweller.FEMALE = " + example.MyWorld.Nested.Dweller.FEMALE);
|
||||
|
||||
// Accessing static member function
|
||||
if (example.MyWorld.Nested.Dweller.count() !== 19) {
|
||||
throw new Error("example.MyWorld.Nested.Dweller.count() should equal 19");
|
||||
}
|
||||
console.log("Accessing static member function: ex.MyWorld.Nested.Dweller.count() = " + example.MyWorld.Nested.Dweller.count());
|
||||
3
Examples/javascript/operator/Makefile
Executable file
3
Examples/javascript/operator/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS =
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/operator/binding.gyp
Normal file
8
Examples/javascript/operator/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
36
Examples/javascript/operator/example.h
Normal file
36
Examples/javascript/operator/example.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* File : example.h */
|
||||
#include <math.h>
|
||||
|
||||
class Complex {
|
||||
private:
|
||||
double rpart, ipart;
|
||||
public:
|
||||
Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
|
||||
Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { }
|
||||
Complex &operator=(const Complex &c) {
|
||||
rpart = c.rpart;
|
||||
ipart = c.ipart;
|
||||
return *this;
|
||||
}
|
||||
Complex operator+(const Complex &c) const {
|
||||
return Complex(rpart+c.rpart, ipart+c.ipart);
|
||||
}
|
||||
Complex operator-(const Complex &c) const {
|
||||
return Complex(rpart-c.rpart, ipart-c.ipart);
|
||||
}
|
||||
Complex operator*(const Complex &c) const {
|
||||
return Complex(rpart*c.rpart - ipart*c.ipart,
|
||||
rpart*c.ipart + c.rpart*ipart);
|
||||
}
|
||||
Complex operator-() const {
|
||||
return Complex(-rpart, -ipart);
|
||||
}
|
||||
|
||||
double re() const { return rpart; }
|
||||
double im() const { return ipart; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
34
Examples/javascript/operator/example.i
Normal file
34
Examples/javascript/operator/example.i
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
#pragma SWIG nowarn=SWIGWARN_IGNORE_OPERATOR_EQ
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* This header file is a little tough to handle because it has overloaded
|
||||
operators and constructors. We're going to try and deal with that here */
|
||||
|
||||
/* This turns the copy constructor in a function ComplexCopy() that can
|
||||
be called */
|
||||
|
||||
%rename(assign) Complex::operator=;
|
||||
%rename(plus) Complex::operator+;
|
||||
%rename(minus) Complex::operator-(const Complex &) const;
|
||||
%rename(uminus) Complex::operator-() const;
|
||||
%rename(times) Complex::operator*;
|
||||
|
||||
/* Now grab the original header file */
|
||||
%include "example.h"
|
||||
|
||||
/* An output method that turns a complex into a short string */
|
||||
%extend Complex {
|
||||
char *toString() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"(%g,%g)", $self->re(), $self->im());
|
||||
return temp;
|
||||
}
|
||||
static Complex* copy(const Complex& c) {
|
||||
return new Complex(c);
|
||||
}
|
||||
};
|
||||
|
||||
1
Examples/javascript/operator/example.js
Normal file
1
Examples/javascript/operator/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
25
Examples/javascript/operator/runme.js
Normal file
25
Examples/javascript/operator/runme.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
var example = require("./example");
|
||||
|
||||
a = new example.Complex(2,3);
|
||||
b = new example.Complex(-5,10);
|
||||
|
||||
console.log ("a =" + a);
|
||||
console.log ("b =" + b);
|
||||
|
||||
c = a.plus(b);
|
||||
|
||||
console.log("c =" + c);
|
||||
console.log("a*b =" + a.times(b));
|
||||
console.log("a-c =" + a.minus(c));
|
||||
|
||||
e = example.Complex.copy(a.minus(c));
|
||||
console.log("e =" + e);
|
||||
|
||||
// Big expression
|
||||
f = a.plus(b).times(c.plus(b.times(e))).plus(a.uminus());
|
||||
console.log("f =" + f);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
3
Examples/javascript/overload/Makefile
Executable file
3
Examples/javascript/overload/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS =
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/overload/binding.gyp
Normal file
8
Examples/javascript/overload/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
28
Examples/javascript/overload/example.h
Normal file
28
Examples/javascript/overload/example.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <iostream>
|
||||
|
||||
void f() {
|
||||
std::cout << "Called f()." << std::endl;
|
||||
}
|
||||
|
||||
void f(int val) {
|
||||
std::cout << "Called f(int)." << std::endl;
|
||||
}
|
||||
void f(int val1, int val2) {
|
||||
std::cout << "Called f(int, int)." << std::endl;
|
||||
}
|
||||
|
||||
void f(const char* s) {
|
||||
std::cout << "Called f(const char*)." << std::endl;
|
||||
}
|
||||
|
||||
void f(bool val) {
|
||||
std::cout << "Called f(bool)." << std::endl;
|
||||
}
|
||||
|
||||
void f(long val) {
|
||||
std::cout << "Called f(long)." << std::endl;
|
||||
}
|
||||
|
||||
void f(double val) {
|
||||
std::cout << "Called f(double)." << std::endl;
|
||||
}
|
||||
16
Examples/javascript/overload/example.i
Normal file
16
Examples/javascript/overload/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/*
|
||||
Note: overloading is implemented in a sloppy way currently
|
||||
i.e., only the number of arguments is taken into conideration
|
||||
for dispatching.
|
||||
To solve the problem one has to rename such conflicting methods.
|
||||
*/
|
||||
%rename(f_double) f(double val);
|
||||
|
||||
%include "example.h"
|
||||
1
Examples/javascript/overload/example.js
Normal file
1
Examples/javascript/overload/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
9
Examples/javascript/overload/runme.js
Normal file
9
Examples/javascript/overload/runme.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
var example = require("./example");
|
||||
|
||||
example.f();
|
||||
example.f(1);
|
||||
example.f(1, 2);
|
||||
example.f("bla");
|
||||
example.f(false);
|
||||
example.f(11111111111);
|
||||
example.f_double(1.0);
|
||||
3
Examples/javascript/pointer/Makefile
Executable file
3
Examples/javascript/pointer/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/pointer/binding.gyp
Normal file
8
Examples/javascript/pointer/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
16
Examples/javascript/pointer/example.cxx
Executable file
16
Examples/javascript/pointer/example.cxx
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(int *x, int *y, int *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void subtract(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
30
Examples/javascript/pointer/example.i
Executable file
30
Examples/javascript/pointer/example.i
Executable file
|
|
@ -0,0 +1,30 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
extern void add(int *, int *, int *);
|
||||
extern void subtract(int *, int *, int *);
|
||||
extern int divide(int, int, int *);
|
||||
%}
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void subtract(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
%apply int *OUTPUT { int *r };
|
||||
extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
1
Examples/javascript/pointer/example.js
Normal file
1
Examples/javascript/pointer/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
35
Examples/javascript/pointer/runme.js
Executable file
35
Examples/javascript/pointer/runme.js
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
var example = require("./example");
|
||||
|
||||
// First create some objects using the pointer library.
|
||||
console.log("Testing the pointer library");
|
||||
a = example.new_intp();
|
||||
b = example.new_intp();
|
||||
c = example.new_intp();
|
||||
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
console.log(" a = " + example.intp_value(a));
|
||||
console.log(" b = " + example.intp_value(b));
|
||||
console.log(" c = " + example.intp_value(c));
|
||||
|
||||
//// Call the add() function with some pointers
|
||||
example.add(a, b, c);
|
||||
|
||||
//
|
||||
//// Now get the result
|
||||
r = example.intp_value(c);
|
||||
console.log(" 37 + 42 = " + r);
|
||||
|
||||
// Clean up the pointers
|
||||
example.delete_intp(a);
|
||||
example.delete_intp(b);
|
||||
example.delete_intp(c);
|
||||
|
||||
//// Now try the typemap library
|
||||
//// This should be much easier. Now how it is no longer
|
||||
//// necessary to manufacture pointers.
|
||||
//"OUTPUT" Mapping is not supported
|
||||
//console.log("Trying the typemap library");
|
||||
//r = example.subtract(37,42);
|
||||
//console.log("37 - 42 =" + r);
|
||||
0
Examples/javascript/pointer/typemaps.i
Normal file
0
Examples/javascript/pointer/typemaps.i
Normal file
3
Examples/javascript/reference/Makefile
Executable file
3
Examples/javascript/reference/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/reference/binding.gyp
Normal file
8
Examples/javascript/reference/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
46
Examples/javascript/reference/example.cxx
Executable file
46
Examples/javascript/reference/example.cxx
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
/* 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 "example.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Vector operator+(const Vector &a, const Vector &b) {
|
||||
Vector r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
r.z = a.z + b.z;
|
||||
return r;
|
||||
}
|
||||
|
||||
char *Vector::print() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorArray::VectorArray(int size) {
|
||||
items = new Vector[size];
|
||||
maxsize = size;
|
||||
}
|
||||
|
||||
VectorArray::~VectorArray() {
|
||||
delete [] items;
|
||||
}
|
||||
|
||||
Vector &VectorArray::operator[](int index) {
|
||||
if ((index < 0) || (index >= maxsize)) {
|
||||
printf("Panic! Array index out of bounds.\n");
|
||||
exit(1);
|
||||
}
|
||||
return items[index];
|
||||
}
|
||||
|
||||
int VectorArray::size() {
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
26
Examples/javascript/reference/example.h
Executable file
26
Examples/javascript/reference/example.h
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Vector {
|
||||
private:
|
||||
double x,y,z;
|
||||
public:
|
||||
Vector() : x(0), y(0), z(0) { };
|
||||
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
||||
friend Vector operator+(const Vector &a, const Vector &b);
|
||||
char *print();
|
||||
};
|
||||
|
||||
class VectorArray {
|
||||
private:
|
||||
Vector *items;
|
||||
int maxsize;
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
Vector &operator[](int);
|
||||
int size();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
42
Examples/javascript/reference/example.i
Executable file
42
Examples/javascript/reference/example.i
Executable file
|
|
@ -0,0 +1,42 @@
|
|||
/* File : example.i */
|
||||
|
||||
/* This file has a few "typical" uses of C++ references. */
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(double x, double y, double z);
|
||||
~Vector();
|
||||
char *print();
|
||||
};
|
||||
|
||||
/* This helper function calls an overloaded operator */
|
||||
%inline %{
|
||||
Vector addv(Vector &a, Vector &b) {
|
||||
return a+b;
|
||||
}
|
||||
%}
|
||||
|
||||
/* Wrapper around an array of vectors class */
|
||||
|
||||
class VectorArray {
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
int size();
|
||||
|
||||
/* This wrapper provides an alternative to the [] operator */
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
return (*$self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
(*$self)[index] = a;
|
||||
}
|
||||
}
|
||||
};
|
||||
1
Examples/javascript/reference/example.js
Normal file
1
Examples/javascript/reference/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
67
Examples/javascript/reference/runme.js
Executable file
67
Examples/javascript/reference/runme.js
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
// This file illustrates the manipulation of C++ references in Javascript.
|
||||
var example = require("./example");
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
console.log("Creating some objects:");
|
||||
a = new example.Vector(3,4,5);
|
||||
b = new example.Vector(10,11,12);
|
||||
|
||||
console.log(" created" + a.print());
|
||||
console.log(" created" + b.print());
|
||||
|
||||
// ----- Call an overloaded operator -----
|
||||
|
||||
// This calls the wrapper we placed around operator+(const Vector &a, const Vector &)
|
||||
// It returns a new allocated object.
|
||||
|
||||
console.log("Adding a+b");
|
||||
c = example.addv(a, b);
|
||||
console.log("a+b = " + c.print());
|
||||
|
||||
|
||||
// TODO: Note: Unless we free the result, a memory leak will occur
|
||||
//delete_Vector(c);
|
||||
|
||||
// ----- Create a vector array -----
|
||||
|
||||
// Note: Using the high-level interface here
|
||||
console.log("Creating an array of vectors");
|
||||
va = new example.VectorArray(10);
|
||||
console.log("va = " + va);
|
||||
|
||||
// ----- Set some values in the array -----
|
||||
|
||||
// These operators copy the value of a and b to the vector array
|
||||
va.set(0,a);
|
||||
va.set(1,b);
|
||||
|
||||
// This will work, but it will cause a memory leak!
|
||||
va.set(2,example.addv(a,b));
|
||||
|
||||
// The non-leaky way to do it
|
||||
//c = addv(a,b);
|
||||
//va.set(3,c);
|
||||
//delete_Vector(c);
|
||||
|
||||
// Get some values from the array
|
||||
|
||||
console.log("Getting some array values");
|
||||
for (i = 0; i < 5; i++) {
|
||||
temp = va.get(i);
|
||||
console.log(i,temp.print());
|
||||
}
|
||||
|
||||
// Watch under resource meter to check on this
|
||||
console.log("Making sure we don't leak memory.");
|
||||
for (i = 0; i < 1000000; i++) {
|
||||
c = va.get(i % 10);
|
||||
}
|
||||
//---------TODO---------
|
||||
//----- Clean up -----
|
||||
//console.log("Cleaning up");
|
||||
|
||||
//example.delete_VectorArray(va);
|
||||
//example.delete_Vector(a);
|
||||
//example.delete_Vector(b);
|
||||
|
||||
3
Examples/javascript/simple/Makefile
Executable file
3
Examples/javascript/simple/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/simple/binding.gyp
Normal file
8
Examples/javascript/simple/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
18
Examples/javascript/simple/example.cxx
Normal file
18
Examples/javascript/simple/example.cxx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
7
Examples/javascript/simple/example.i
Normal file
7
Examples/javascript/simple/example.i
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
%}
|
||||
1
Examples/javascript/simple/example.js
Normal file
1
Examples/javascript/simple/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
26
Examples/javascript/simple/runme.js
Executable file
26
Examples/javascript/simple/runme.js
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
var example = require("./example");
|
||||
|
||||
/* Call our gcd() function */
|
||||
|
||||
x = 42;
|
||||
y = 105;
|
||||
g = example.gcd(x,y);
|
||||
console.log("GCD of x and y is=" + g);
|
||||
|
||||
/* Manipulate the Foo global variable */
|
||||
|
||||
/* Output its current value */
|
||||
console.log("Global variable Foo=" + example.Foo);
|
||||
|
||||
/* Change its value */
|
||||
example.Foo = 3.1415926;
|
||||
|
||||
/* See if the change took effect */
|
||||
console.log("Variable Foo changed to=" + example.Foo);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
3
Examples/javascript/template/Makefile
Executable file
3
Examples/javascript/template/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS =
|
||||
|
||||
include ../example.mk
|
||||
8
Examples/javascript/template/binding.gyp
Normal file
8
Examples/javascript/template/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
32
Examples/javascript/template/example.h
Normal file
32
Examples/javascript/template/example.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* File : example.h */
|
||||
|
||||
// Some template definitions
|
||||
|
||||
template<class T> T max(T a, T b) { return a>b ? a : b; }
|
||||
|
||||
template<class T> class vector {
|
||||
T *v;
|
||||
int sz;
|
||||
public:
|
||||
vector(int _sz) {
|
||||
v = new T[_sz];
|
||||
sz = _sz;
|
||||
}
|
||||
T &get(int index) {
|
||||
return v[index];
|
||||
}
|
||||
void set(int index, T &val) {
|
||||
v[index] = val;
|
||||
}
|
||||
#ifdef SWIG
|
||||
%extend {
|
||||
T getitem(int index) {
|
||||
return $self->get(index);
|
||||
}
|
||||
void setitem(int index, T val) {
|
||||
$self->set(index,val);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
17
Examples/javascript/template/example.i
Normal file
17
Examples/javascript/template/example.i
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
/* Now instantiate some specific template declarations */
|
||||
|
||||
%template(maxint) max<int>;
|
||||
%template(maxdouble) max<double>;
|
||||
%template(vecint) vector<int>;
|
||||
%template(vecdouble) vector<double>;
|
||||
|
||||
1
Examples/javascript/template/example.js
Normal file
1
Examples/javascript/template/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
30
Examples/javascript/template/runme.js
Normal file
30
Examples/javascript/template/runme.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
var example = require("./example");
|
||||
|
||||
//Call some templated functions
|
||||
console.log(example.maxint(3,7));
|
||||
console.log(example.maxdouble(3.14,2.18));
|
||||
|
||||
// Create some class
|
||||
|
||||
iv = new example.vecint(100);
|
||||
dv = new example.vecdouble(1000);
|
||||
|
||||
for(i=0;i<=100;i++)
|
||||
iv.setitem(i,2*i);
|
||||
|
||||
for(i=0;i<=1000;i++)
|
||||
dv.setitem(i, 1.0/(i+1));
|
||||
|
||||
sum = 0;
|
||||
for(i=0;i<=100;i++)
|
||||
sum = sum + iv.getitem(i);
|
||||
|
||||
console.log(sum);
|
||||
|
||||
sum = 0.0;
|
||||
for(i=0;i<=1000;i++)
|
||||
sum = sum + dv.getitem(i);
|
||||
console.log(sum);
|
||||
|
||||
delete iv;
|
||||
delete dv;
|
||||
3
Examples/javascript/variables/Makefile
Executable file
3
Examples/javascript/variables/Makefile
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
SRCS = example.cxx
|
||||
|
||||
include ../example.mk
|
||||
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);
|
||||
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();
|
||||
%}
|
||||
|
||||
1
Examples/javascript/variables/example.js
Normal file
1
Examples/javascript/variables/example.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./build/Release/example");
|
||||
68
Examples/javascript/variables/runme.js
Executable file
68
Examples/javascript/variables/runme.js
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
var example = require("./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 printed from Javascript)");
|
||||
console.log("ivar = " + example.ivar);
|
||||
console.log("svar = " + example.svar);
|
||||
console.log("lvar = " + example.lvar);
|
||||
console.log("uivar = " + example.uivar);
|
||||
console.log("usvar = " + example.usvar);
|
||||
console.log("ulvar = " + example.ulvar);
|
||||
console.log("scvar = " + example.scvar);
|
||||
console.log("ucvar = " + example.ucvar);
|
||||
console.log("fvar = " + example.fvar);
|
||||
console.log("dvar = " + example.dvar);
|
||||
console.log("cvar = " + example.cvar);
|
||||
console.log("strvar = " + example.strvar);
|
||||
console.log("cstrvar = " + example.cstrvar);
|
||||
console.log("iptrvar = " + example.iptrvar);
|
||||
console.log("name = " + example.name);
|
||||
console.log("ptptr = " + example.ptptr + ": " + example.Point_print(example.ptptr));
|
||||
console.log("pt = " + example.pt + ": " + example.Point_print(example.pt));
|
||||
|
||||
|
||||
console.log("\nVariables (values printed 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.");
|
||||
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));
|
||||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
/* check C++ delete keyword is okay in C wrappers */
|
||||
|
||||
#pragma SWIG nowarn=SWIGWARN_PARSE_KEYWORD
|
||||
%warnfilter(SWIGWARN_PARSE_KEYWORD) delete;
|
||||
|
||||
#if !defined(SWIGOCTAVE) /* Octave compiles wrappers as C++ */
|
||||
#if !defined(SWIGOCTAVE) && !defined(SWIG_JAVASCRIPT_V8) /* Octave and Javascript/v8 compiles wrappers as C++ */
|
||||
|
||||
%inline %{
|
||||
struct delete {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
/* check C++ delete keyword is okay in C wrappers */
|
||||
|
||||
#if !defined(SWIGOCTAVE) /* Octave compiles wrappers as C++ */
|
||||
%warnfilter(SWIGWARN_PARSE_KEYWORD) delete;
|
||||
|
||||
#if !defined(SWIGOCTAVE) && !defined(SWIG_JAVASCRIPT_V8) /* Octave and Javascript/v8 compiles wrappers as C++ */
|
||||
|
||||
%inline %{
|
||||
double delete(double d) { return d; }
|
||||
|
|
|
|||
|
|
@ -588,6 +588,7 @@ C_TEST_CASES += \
|
|||
global_functions \
|
||||
immutable_values \
|
||||
inctest \
|
||||
infinity \
|
||||
integers \
|
||||
keyword_rename \
|
||||
lextype \
|
||||
|
|
@ -614,6 +615,7 @@ C_TEST_CASES += \
|
|||
simple_array \
|
||||
sizeof_pointer \
|
||||
sneaky1 \
|
||||
string_simple \
|
||||
struct_rename \
|
||||
struct_initialization \
|
||||
typedef_struct \
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ public:
|
|||
|
||||
%include "std_vector.i"
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGR) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGR) || defined(SWIGOCTAVE) || defined(SWIGRUBY) || defined(SWIGJAVASCRIPT)
|
||||
#define SWIG_GOOD_VECTOR
|
||||
%ignore std::vector<Space::Flow>::vector(size_type);
|
||||
%ignore std::vector<Space::Flow>::resize(size_type);
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ namespace TopLevel
|
|||
%include <std_string.i>
|
||||
|
||||
// nspace feature only supported by these languages
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD) || defined(SWIGLUA)
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD) || defined(SWIGLUA) || defined(SWIGJAVASCRIPT)
|
||||
%nspace TopLevel::Bar::Foo;
|
||||
%nspace TopLevel::Bar::FooBar;
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ namespace TopLevel
|
|||
%include <std_string.i>
|
||||
|
||||
// nspace feature only supported by these languages
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD) || defined(SWIGLUA)
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGD) || defined(SWIGLUA) || defined(SWIGJAVASCRIPT)
|
||||
%nspace TopLevel::A::Foo;
|
||||
%nspace TopLevel::B::Foo;
|
||||
#else
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue