Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel
Conflicts: .project COPYRIGHT Doc/Manual/style.css Examples/Makefile.in Examples/test-suite/common.mk Lib/typemaps/strings.swg Makefile.in Source/DOH/fio.c Source/Makefile.am Source/Modules/emit.cxx Source/Modules/javascript.cxx configure.in git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13764 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
360ef44e09
commit
050219d998
136 changed files with 7987 additions and 141 deletions
72
.cproject
Normal file
72
.cproject
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?fileVersion 4.0.0?>
|
||||
|
||||
<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
|
||||
<storageModule moduleId="org.eclipse.cdt.core.settings">
|
||||
<cconfiguration id="cdt.managedbuild.toolchain.gnu.base.449123076">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.base.449123076" moduleId="org.eclipse.cdt.core.settings" name="Default">
|
||||
<externalSettings/>
|
||||
<extensions>
|
||||
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration buildProperties="" description="" id="cdt.managedbuild.toolchain.gnu.base.449123076" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
|
||||
<folderInfo id="cdt.managedbuild.toolchain.gnu.base.449123076.1287656342" name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.base.25396580" name="cdt.managedbuild.toolchain.gnu.base" superClass="cdt.managedbuild.toolchain.gnu.base">
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.target.gnu.platform.base.1945159706" name="Debug Platform" osList="linux,hpux,aix,qnx" superClass="cdt.managedbuild.target.gnu.platform.base"/>
|
||||
<builder id="cdt.managedbuild.target.gnu.builder.base.2106583443" incrementalBuildTarget="" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.archiver.base.350702521" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.177179889" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.98665148" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.c.compiler.base.1231243742" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.142605289" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.c.linker.base.1242885489" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.base.119708000" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1425572017" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
|
||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||
</inputType>
|
||||
</tool>
|
||||
<tool id="cdt.managedbuild.tool.gnu.assembler.base.764500254" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.base">
|
||||
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1968997645" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
|
||||
</tool>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="scannerConfiguration">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<project id="gsoc2012-javascript.null.2075198600" name="gsoc2012-javascript"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
<storageModule moduleId="refreshScope" versionNumber="2">
|
||||
<configuration configurationName="Default">
|
||||
<resource resourceType="PROJECT" workspacePath="/gsoc2012-javascript"/>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">
|
||||
<buildTargets>
|
||||
<target name="cpp_static.cpptest" path="Examples/test-suite/javascript" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>cpp_static.cpptest</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
</buildTargets>
|
||||
</storageModule>
|
||||
</cproject>
|
||||
16
.project
16
.project
|
|
@ -5,7 +5,23 @@
|
|||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<triggers>full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
|
|||
|
|
@ -15,8 +15,9 @@ Active SWIG Developers:
|
|||
Olly Betts (olly@survex.com) (PHP)
|
||||
Joseph Wang (joequant@gmail.com) (R)
|
||||
Xavier Delacour (xavier.delacour@gmail.com) (Octave)
|
||||
David Nadlinger (code@klickverbot.at) (D)
|
||||
Oliver Buchtala (Javascript)
|
||||
David Nadlinger (code@klickverbot.at)
|
||||
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)
|
||||
|
|
@ -61,7 +62,6 @@ Past SWIG developers and major contributors include:
|
|||
Ian Lance Taylor (Go)
|
||||
Vadim Zeitlin (PCRE)
|
||||
Stefan Zager (szager@gmail.com) (Python)
|
||||
Oliver Buchtala (Javascript)
|
||||
|
||||
Past contributors include:
|
||||
James Michael DuPont, Clark McGrew, Dustin Mitchell, Ian Cooke, Catalin Dumitrescu, Baran
|
||||
|
|
|
|||
770
Doc/Manual/Javascript.html
Normal file
770
Doc/Manual/Javascript.html
Normal file
|
|
@ -0,0 +1,770 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="Content-Style-Type" content="text/css" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
<title>SWIG AND JAVASCRIPT</title>
|
||||
<link rel="stylesheet" href="style.css" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="header">
|
||||
<h1 class="title">SWIG AND JAVASCRIPT</h1>
|
||||
</div>
|
||||
<div class="sectiontoc">
|
||||
<div id="TOC">
|
||||
<ul>
|
||||
<li><a href="#overview">Overview</a></li>
|
||||
<li><a href="#preliminaries">Preliminaries</a><ul>
|
||||
<li><a href="#using-the-module">Using the module</a></li>
|
||||
<li><a href="#how-does-javascript-talk-to-c">How does Javascript talk to C++?</a></li>
|
||||
<li><a href="#wrapper-functions">Wrapper functions</a></li>
|
||||
<li><a href="#variable-linking">Variable Linking</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#a-tour-of-basic-cc-wrapping">A tour of basic C/C++ wrapping</a><ul>
|
||||
<li><a href="#modules">Modules</a></li>
|
||||
<li><a href="#global-variables">Global variables</a></li>
|
||||
<li><a href="#constants-and-enums">Constants and enums</a><ul>
|
||||
<li><a href="#inside-the-class">Inside the class</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#pointers">Pointers</a><ul>
|
||||
<li><a href="#c-classes">C++ classes</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#c-inheritance">C++ inheritance</a></li>
|
||||
<li><a href="#c-overloaded-functions">C++ overloaded functions</a></li>
|
||||
<li><a href="#c-operators">C++ operators</a></li>
|
||||
<li><a href="#c-namespaces">C++ namespaces:</a></li>
|
||||
<li><a href="#c-templates">C++ templates</a></li>
|
||||
<li><a href="#exception-handling">Exception handling</a></li>
|
||||
<li><a href="#how-to-use-generated-modules">How to use generated modules?</a><ul>
|
||||
<li><a href="#basic-mechanism">Basic Mechanism</a><ul>
|
||||
<li><a href="#creating-the-context">Creating the context</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#calling-module-initializer">Calling module initializer</a></li>
|
||||
<li><a href="#evaluate-javascript">Evaluate Javascript</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#typemaps">Typemaps</a></li>
|
||||
<li><a href="#javascript-typemaps">Javascript typemaps</a></li>
|
||||
<li><a href="#typemap-variables">Typemap variables</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#javascript-specification-of-a-code-generator-for-jsc">Javascript: Specification of a Code Generator for JSC</a></li>
|
||||
<li><a href="#top-level-structure">Top Level structure</a><ul>
|
||||
<li><a href="#includes">INCLUDES</a></li>
|
||||
<li><a href="#helper_functions"><code>HELPER_FUNCTIONS</code></a></li>
|
||||
<li><a href="#function_wrappers"><code>FUNCTION_WRAPPERS</code></a></li>
|
||||
<li><a href="#member-functions">Member Functions</a></li>
|
||||
<li><a href="#setproperty">Setproperty</a></li>
|
||||
<li><a href="#getproperty">Getproperty</a></li>
|
||||
<li><a href="#global-functions">Global Functions</a></li>
|
||||
<li><a href="#variable-declaration">Variable declaration</a></li>
|
||||
<li><a href="#constructor">Constructor</a></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h1 id="overview"><a href="#TOC">Overview</a></h1>
|
||||
<p>This chapter describes SWIG support for Javascript. The module is designed to support JavascriptCore and V8 as target engine. Currently only JavascriptCore support is implemented. JavaScriptCore is the built-in JavaScript engine for WebKit, whereas V8 is the engine used by Chromium.</p>
|
||||
<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. C++, on the other hand, is statically typed, compiled, general purpose programming language. The approach I followed here is "Test driven" where I have written the examples/test-cases to be supported for Javascript one-by-one and implemented the required module files in parallel. The support for Javascript would be added similar to other supported target languages in swig. Swig comes with an "Examples" directory for Javascript like other supported language. The directory contains examples for every supported feature of the target language. There is also a test-suite directory for javascript which contains additional tests.</p>
|
||||
<h1 id="preliminaries"><a href="#TOC">Preliminaries</a></h1>
|
||||
<p>In order to use this module, you will need to have installed javascriptcore and you can install it by installing package libwebkit-dev You can find out some necessary compiler/linker flag by</p>
|
||||
<pre><code>pkg-config javascriptcoregtk-1.0 --cflags --libs
|
||||
</code></pre>
|
||||
<h2 id="using-the-module"><a href="#TOC">Using the module</a></h2>
|
||||
<p>To generate an extension for JavascriptCore one would call swig as follows</p>
|
||||
<pre><code><br />swig -c++ -javascript -jsc example.i
|
||||
</code></pre>
|
||||
<p>This generates a C++ source file containing the wrapper.</p>
|
||||
<h2 id="how-does-javascript-talk-to-c"><a href="#TOC">How does Javascript talk to C++?</a></h2>
|
||||
<p>JavascriptCore provides a C-API which allows to extend a Javascript interpreter with native methods and structures. Normally, this is used to implement the builtin features of the language. However, by extending the interpreter, it is also possible to add your own commands and variables. A reference manual of this API can be found <a href="http://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/_index.html">here</a>.</p>
|
||||
<p>Typically, when you add a new command to the javascript interpreter you need to do two things: first you need to write a special "wrapper" function that serves as the glue between the interpreter and the underlying C function. Then you need to give the interpreter information about the wrapper by providing details about the name of the function, arguments, and so forth. The next few sections illustrate the process.</p>
|
||||
<h2 id="wrapper-functions"><a href="#TOC">Wrapper functions</a></h2>
|
||||
<p>Suppose you have an ordinary C function like this :</p>
|
||||
<pre><code><br />int fact(int n) {
|
||||
if (n <= 1) return 1;
|
||||
else return n*fact(n-1);
|
||||
}
|
||||
</code></pre>
|
||||
<p>In order to access this function from a scripting language, it is necessary to write a special "wrapper" function that serves as the glue between the scripting language and the underlying C function. A wrapper function must do three things :</p>
|
||||
<ul>
|
||||
<li>Gather function arguments and make sure they are valid.</li>
|
||||
<li>Call the C function.</li>
|
||||
<li>Convert the return value into a form recognized by the javascript.</li>
|
||||
</ul>
|
||||
<p>As an example, the javascript wrapper function for the fact() function above example might look like the following :</p>
|
||||
<pre><code><br />JSValueRef wrap_fact(JSContextRef context, JSObjectRef function, JSObjectRef globalobj, size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
||||
{
|
||||
int arg1 = (int)JSValueToNumber(context, argv[0], NULL);
|
||||
int arg2 = (int)JSValueToNumber(context, argv[1], NULL);
|
||||
int result = (int)fact(arg1,arg2);
|
||||
JSValueRef jsresult = JSValueMakeNumber(context, result);
|
||||
return jsresult;
|
||||
}
|
||||
</code></pre>
|
||||
<p>Once you have created a wrapper function, the final step is to tell the javascript about the new function. This is done by register function called by the javascript when the module is loaded. For example, adding the above function to the javascript interpreter requires code like the following :</p>
|
||||
<pre><code><br />bool jsc_registerFunction(JSGlobalContextRef context, JSObjectRef object,
|
||||
const char* functionName, JSObjectCallAsFunctionCallback callback)
|
||||
{
|
||||
JSStringRef js_globalvarsclassname = JSStringCreateWithUTF8CString(globalvarsclassname);
|
||||
JSObjectSetProperty(context,js_globalvarsclassname,JSObjectMakeFunctionWithCallback(context,
|
||||
js_globalvarsclassname, callback), kJSPropertyAttributeNone,NULL);
|
||||
JSStringRelease(jsstring);
|
||||
return true;
|
||||
}
|
||||
|
||||
int example_init(JSContextRef context) {
|
||||
JSObjectRef global;
|
||||
...
|
||||
jsc_registerFunction(context, global, "fact", wrap_fact);
|
||||
...
|
||||
}
|
||||
</code></pre>
|
||||
<p>When executed, javascript will now have a new command called "fact" that you can use like any other Javascript command. Although the process of adding a new function to javascript has been illustrated, the procedure is almost identical for Perl and Python. Both require special wrappers to be written and both need additional initialization code.</p>
|
||||
<h2 id="variable-linking"><a href="#TOC">Variable Linking</a></h2>
|
||||
<p>Variable linking refers to the problem of mapping a C/C++ global variable to a variable in the scripting language interpreter. For example, suppose you had the following variable:</p>
|
||||
<pre><code><br />double Foo = 3.5;
|
||||
</code></pre>
|
||||
<p>To provide such access, variables are commonly manipulated using a pair of get/set functions. For example, whenever the value of a variable is read, a "get" function is invoked. Similarly, whenever the value of a variable is changed, a "set" function is called.</p>
|
||||
<pre><code><br />bool Foo_set(JSContextRef context, JSObjectRef globalobj, JSStringRef propertyName, JSValueRef value,
|
||||
JSValueRef* exception)
|
||||
{
|
||||
JSValueRef jsresult;
|
||||
double arg1 = (double)JSValueToNumber(context, value, NULL);
|
||||
Foo = arg1;
|
||||
jscresult = JSValueMakeUndefined(context);
|
||||
return jsresult;
|
||||
}
|
||||
|
||||
JSValueRef Foo_get(JSContextRef context, JSObjectRef globalobj, JSStringRef propertyName, JSValueRef* exception)
|
||||
{
|
||||
JSValueRef jsresult;
|
||||
double result = (double)Foo;
|
||||
jsresult = JSValueMakeNumber(context, result);
|
||||
return jsresult;
|
||||
}
|
||||
</code></pre>
|
||||
<p>In many languages, calls to the get/set functions can be attached to evaluation and assignment operators. Therefore, evaluating a variable such as Foo might implicitly call the get function. Similarly, typing Foo = 4 would call the underlying set function to change the value.</p>
|
||||
<h1 id="a-tour-of-basic-cc-wrapping"><a href="#TOC">A tour of basic C/C++ wrapping</a></h1>
|
||||
<p>By default, SWIG tries to build a very natural javascript interface to your C/C++ code. Functions are wrapped as functions, classes are wrapped as classes, and so forth. This section briefly covers the essential aspects of this wrapping.</p>
|
||||
<h2 id="modules"><a href="#TOC">Modules</a></h2>
|
||||
<p>The SWIG %module directive specifies the name of the Javascript module. If you specify <code>%module example</code>, then everything is wrapped into a Javascript 'example' module. Underneath the covers, this module consists of a cpp source file example.cpp. When choosing a module name, make sure you don't use the same name as a built-in Javascript command or standard module name.</p>
|
||||
<h2 id="global-variables"><a href="#TOC">Global variables</a></h2>
|
||||
<p>C/C++ global variables are fully supported by SWIG. However, the underlying mechanism is somewhat different than you might expect due to the way that javascript works.</p>
|
||||
<pre><code><br />// SWIG interface file with global variables
|
||||
%module example
|
||||
...
|
||||
%inline %{
|
||||
extern double Foo;
|
||||
extern int gcd(int x, int y);
|
||||
%}
|
||||
...
|
||||
</code></pre>
|
||||
<p>Now look at the javascript:</p>
|
||||
<pre><code><br />print("Global variable Foo=" + example.Foo);
|
||||
example.Foo = 3.1415926;
|
||||
print("Variable Foo changed to=" + example.Foo);
|
||||
print("GCD of x and y is=" + example.gcd(x,y));
|
||||
</code></pre>
|
||||
<h2 id="constants-and-enums"><a href="#TOC">Constants and enums</a></h2>
|
||||
<p>C/C++ constants are installed as javascript objects containing the appropriate value. To create a constant, use #define, enum, or the %constant directive. For example:</p>
|
||||
<pre><code><br />#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
%constant int iconst = 37;
|
||||
</code></pre>
|
||||
<p>In javascript they are treated as:</p>
|
||||
<pre><code><br />print("ICONST = " + example.ICONST + " (should be 42)\n");
|
||||
print("FCONST = " + example.FCONST + " (should be 2.1828)\n");
|
||||
print("iconst = " + example.iconst + " (should be 37)\n");
|
||||
</code></pre>
|
||||
<p>For enums, make sure that the definition of the enumeration actually appears in a header file or in the wrapper file somehow---if you just stick an enum in a SWIG interface without also telling the C compiler about it, the wrapper code won't compile. Enums are treated as constants.So if we have enums in c++ as:</p>
|
||||
<pre><code><br />void enum_test(color c, Foo::speed s);
|
||||
</code></pre>
|
||||
<p>In javascript they are treated as:</p>
|
||||
<pre><code><br />example.enum_test(example.RED, example.Foo.IMPULSE);
|
||||
example.enum_test(example.BLUE, example.Foo.WARP);
|
||||
example.enum_test(example.GREEN, example.Foo.LUDICROUS);
|
||||
</code></pre>
|
||||
<h3 id="inside-the-class"><a href="#TOC">Inside the class</a></h3>
|
||||
<p>For class enums as below:</p>
|
||||
<pre><code><br />class Foo {
|
||||
public:
|
||||
Foo() { }
|
||||
enum speed { IMPULSE, WARP, LUDICROUS };
|
||||
}
|
||||
</code></pre>
|
||||
<p>In javascript they are treated as:</p>
|
||||
<pre><code><br />print(" Foo_IMPULSE =" + example.Foo.IMPULSE);
|
||||
print(" Foo_WARP =" + example.Foo.WARP);
|
||||
print(" Foo_LUDICROUS =" + example.Foo.LUDICROUS);
|
||||
</code></pre>
|
||||
<h2 id="pointers"><a href="#TOC">Pointers</a></h2>
|
||||
<p>C/C++ pointers are fully supported by SWIG. Furthermore, SWIG has no problem working with incomplete type information. Here is a rather simple interface</p>
|
||||
<pre><code><br />/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
extern void add(int *, int *, int *);
|
||||
%}
|
||||
</code></pre>
|
||||
<p>When wrapped, you will be able to use the functions in a natural way from javascript. For example:</p>
|
||||
<pre><code><br />// Call the add() function with some pointers
|
||||
example.add(a, b, c);
|
||||
</code></pre>
|
||||
<p>// In javascript the code look like as:</p>
|
||||
<pre><code><br />a = example.new_intp();
|
||||
example.intp_assign(a,37);
|
||||
</code></pre>
|
||||
<ul>
|
||||
<li>The first one creates an int-pointer instance.</li>
|
||||
<li>The second one assigns it the value 37.</li>
|
||||
</ul>
|
||||
<h3 id="c-classes"><a href="#TOC">C++ classes</a></h3>
|
||||
<p>C++ classes are wrapped by javascript classes as well. For example, if you have this class,</p>
|
||||
<pre><code><br />class Circle
|
||||
{
|
||||
public:
|
||||
Circle();
|
||||
Circle(double r);
|
||||
double area();
|
||||
double radius;
|
||||
};
|
||||
</code></pre>
|
||||
<p>you can use it in javascript like this:</p>
|
||||
<pre><code><br />print("Creating some objects:");
|
||||
c = new example.Circle(10);
|
||||
print("area = " + c.area());
|
||||
</code></pre>
|
||||
<p>Class data members are accessed in the same manner as C structures.</p>
|
||||
<p>Static class members and functions are mapped to javascript in a straight-forward manner:</p>
|
||||
<pre><code><br />class Spam {
|
||||
public:
|
||||
static void foo();
|
||||
static int bar;
|
||||
};
|
||||
</code></pre>
|
||||
<p>In javascript, the static member can be access in this way:</p>
|
||||
<pre><code><br />// ----- Access a static member -----
|
||||
print("\nA access of static member is" + example.Spam.Foo); // access static member as properties of the class object.
|
||||
</code></pre>
|
||||
<h2 id="c-inheritance"><a href="#TOC">C++ inheritance</a></h2>
|
||||
<p>SWIG is fully aware of issues related to C++ inheritance. Therefore, if you have classes like this</p>
|
||||
<pre><code><br />class A {
|
||||
public:
|
||||
void foo();
|
||||
virtual void bar();
|
||||
};
|
||||
class B: public A {
|
||||
public:
|
||||
virtual void bar();
|
||||
};
|
||||
</code></pre>
|
||||
<p>Those classes are wrapped into a hierarchy of javascript classes that reflect the same inheritance structure. All of the usual javascript utility functions work normally:</p>
|
||||
<pre><code><br />var a = new example.A();
|
||||
a.foo();
|
||||
a.bar();
|
||||
var b = new example.B();
|
||||
b.foo();
|
||||
b.bar();
|
||||
print("b.cPtr = " + b.getCPtr());
|
||||
</code></pre>
|
||||
<h2 id="c-overloaded-functions"><a href="#TOC">C++ overloaded functions</a></h2>
|
||||
<p>C++ overloaded functions, methods, and constructors are mostly supported by SWIG. For example, if you have two functions like this:</p>
|
||||
<pre><code><br />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;
|
||||
}
|
||||
</code></pre>
|
||||
<p>You can use them in javascript in a straightforward manner:</p>
|
||||
<pre><code><br />example.f(1);
|
||||
example.f(1, 2);
|
||||
example.f("bla");
|
||||
</code></pre>
|
||||
<h2 id="c-operators"><a href="#TOC">C++ operators</a></h2>
|
||||
<p>Certain C++ overloaded operators can be handled automatically by SWIG. Though, in javascript operator overloading is not possible. Instead one has to make use of the <code>%rename</code> feature.</p>
|
||||
<p>For example, consider a class like this:</p>
|
||||
<pre><code><br />/* 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; }
|
||||
};
|
||||
</code></pre>
|
||||
<p>When wrapped, it works like you expect:</p>
|
||||
<pre><code><br />a = new example.Complex(2,3);
|
||||
b = new example.Complex(-5,10);
|
||||
|
||||
print ("a =" + a);
|
||||
print ("b =" + b);
|
||||
|
||||
c = a.plus(b);
|
||||
|
||||
print("c =" + c);
|
||||
print("a*b =" + a.times(b));
|
||||
print("a-c =" + a.minus(c));
|
||||
|
||||
e = example.Complex.copy(a.minus(c));
|
||||
print("e =" + e);
|
||||
|
||||
// Big expression
|
||||
f = a.plus(b).times(c.plus(b.times(e))).plus(a.uminus());
|
||||
print("f =" + f);
|
||||
</code></pre>
|
||||
<p>One restriction with operator overloading support is that SWIG is not able to fully handle operators that aren't defined as part of the class. For example, if you had code like this</p>
|
||||
<pre><code><br />class Complex {
|
||||
...
|
||||
friend Complex operator+(double, const Complex &c);
|
||||
...
|
||||
};
|
||||
</code></pre>
|
||||
<p>then SWIG ignores it and issues a warning. You can still wrap the operator, but you may have to encapsulate it in a special function. For example:</p>
|
||||
<p>%rename(Complex_add_dc) operator+(double, const Complex &);</p>
|
||||
<p>There are ways to make this operator appear as part of the class using the %extend directive.</p>
|
||||
<h2 id="c-namespaces"><a href="#TOC">C++ namespaces:</a></h2>
|
||||
<p>SWIG is aware of C++ namespaces, but namespace names do not appear in the module nor do namespaces result in a module that is broken up into submodules or packages. For example, if you have a file like this,</p>
|
||||
<pre><code><br />%module example
|
||||
namespace nspace {
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
class Circle
|
||||
{
|
||||
public:
|
||||
Circle();
|
||||
Circle(double r);
|
||||
double area();
|
||||
double radius;
|
||||
};
|
||||
}
|
||||
</code></pre>
|
||||
<p>for namespaces, you use the %feature directive in interface file. %feature attaches a new attribute to any parse tree node that matches given prototype.</p>
|
||||
<pre><code><br />/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
%feature("nspace", 1);
|
||||
%include "example.h"
|
||||
</code></pre>
|
||||
<p>it works in javascript as follows:</p>
|
||||
<pre><code><br />print("Global variable Foo=" + example.nspace.Foo);
|
||||
example.nspace.Foo = 5;
|
||||
print("Variable Foo changed to " + example.nspace.Foo);
|
||||
print("GCD of number 6,18 is " + example.nspace.gcd(6,18));
|
||||
print("Creating some objects:");
|
||||
c = new example.nspace.Circle(10);
|
||||
print("area = " + c.area());
|
||||
</code></pre>
|
||||
<p>If your program has more than one namespace, name conflicts (if any) can be resolved using %rename For example:</p>
|
||||
<pre><code><br />%rename(Bar_spam) Bar::spam;
|
||||
namespace Foo {
|
||||
int spam();
|
||||
}
|
||||
namespace Bar {
|
||||
int spam();
|
||||
}
|
||||
</code></pre>
|
||||
<p>If you have more than one namespace and your want to keep their symbols separate, consider wrapping them as separate SWIG modules. For example, make the module name the same as the namespace and create extension modules for each namespace separately. If your program utilizes thousands of small deeply nested namespaces each with identical symbol names, well, then you get what you deserve. SWIG resolves overloaded functions and methods using a disambiguation scheme that ranks and sorts declarations according to a set of type-precedence rules. The order in which declarations appear in the input does not matter except in situations where ambiguity arises--in this case, the first declaration takes precedence.</p>
|
||||
<h2 id="c-templates"><a href="#TOC">C++ templates</a></h2>
|
||||
<p>C++ templates don't present a huge problem for SWIG. However, in order to create wrappers, you have to tell SWIG to create wrappers for a particular template instantiation. To do this, you use the %template directive. For example:</p>
|
||||
<pre><code><br />/* 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>;
|
||||
</code></pre>
|
||||
<p>In javascript:</p>
|
||||
<pre><code><br />//Call some templated functions
|
||||
print(example.maxint(3,7));
|
||||
print(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);
|
||||
|
||||
print(sum);
|
||||
|
||||
sum = 0.0;
|
||||
for(i=0;i<=1000;i++)
|
||||
sum = sum + dv.getitem(i);
|
||||
print(sum);
|
||||
</code></pre>
|
||||
<h2 id="exception-handling"><a href="#TOC">Exception handling</a></h2>
|
||||
<p>The SWIG %exception directive can be used to create a user-definable exception handler for converting exceptions in your C/C++ program into javascript exceptions. The chapter on customization features contains more details, but suppose you have a C++ class like the following:</p>
|
||||
<p>Since several methods in this class can throw an exception for an out-of-bounds access, you might want to catch this in the javascript extension by writing the following in an interface file:</p>
|
||||
<pre><code><br />/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
%include "std_string.i"
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
</code></pre>
|
||||
<p>Actually in JS there is no support for typed exceptions.For now there is support for integer and string exception. Example for integer exception</p>
|
||||
<pre><code><br />JSValueRef jsc_gcd(JSContextRef context, JSObjectRef function, JSObjectRef globalobj, size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
||||
{
|
||||
int arg1 = (int)JSValueToNumber(context, argv[0], NULL);
|
||||
int arg2 = (int)JSValueToNumber(context, argv[1], NULL);
|
||||
*exception = JSValueMakeNumber(context, 13);
|
||||
int result = (int)gcd(arg1,arg2);
|
||||
JSValueRef jsresult = JSValueMakeNumber(context, result);
|
||||
</code></pre>
|
||||
<p>and for string exception:</p>
|
||||
<pre><code><br />JSValueRef wrap_gcd(JSContextRef context, JSObjectRef function, JSObjectRef globalobj, size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
||||
{
|
||||
int arg1 = (int)JSValueToNumber(context, argv[0], NULL);
|
||||
int arg2 = (int)JSValueToNumber(context, argv[1], NULL);
|
||||
JSStringRef message = JSStringCreateWithUTF8CString("This is a test error.");
|
||||
*exception = JSValueMakeString(context, message);
|
||||
JSStringRelease(message);
|
||||
int result = (int)gcd(arg1,arg2);
|
||||
JSValueRef jscresult = JSValueMakeNumber(context, result);
|
||||
return jsresult;
|
||||
}
|
||||
</code></pre>
|
||||
<h2 id="how-to-use-generated-modules"><a href="#TOC">How to use generated modules?</a></h2>
|
||||
<p>Basically there is no standard extension mechanism in Javascript. We provided a custom interpreter with extension abilities. If JSC is embedded into a custom application, one has to make use of a generated module initializer function that allows easy extension of interpreter. The basic approach is as follows:</p>
|
||||
<h3 id="basic-mechanism"><a href="#TOC">Basic Mechanism</a></h3>
|
||||
<ul>
|
||||
<li>Creating the context</li>
|
||||
<li>Calling module initializer</li>
|
||||
<li>Evaluate Javascript</li>
|
||||
</ul>
|
||||
<h4 id="creating-the-context"><a href="#TOC">Creating the context</a></h4>
|
||||
<pre><code><br />JSGlobalContextRef context = JSGlobalContextCreate(NULL);
|
||||
JSObjectRef globalObject = JSContextGetGlobalObject(context);
|
||||
...
|
||||
</code></pre>
|
||||
<h3 id="calling-module-initializer"><a href="#TOC">Calling module initializer</a></h3>
|
||||
<pre><code><br /> extern int example_init(JSGlobalContextRef context);
|
||||
...
|
||||
example_init(context);
|
||||
...
|
||||
</code></pre>
|
||||
<h3 id="evaluate-javascript"><a href="#TOC">Evaluate Javascript</a></h3>
|
||||
<pre><code><br />// Evaluate the javascript
|
||||
char* scriptContent = jsccreateStringWithContentsOfFile(scriptPath.c_str());
|
||||
JSStringRef jsScript;
|
||||
if(!scriptContent) {
|
||||
printf("FAIL: runme script could not be loaded.\n");
|
||||
failed = 1;
|
||||
}
|
||||
else {
|
||||
JSValueRef ex;
|
||||
jsScript = JSStringCreateWithUTF8CString(scriptContent);
|
||||
JSValueRef jsResult = JSEvaluateScript(context, jsScript, 0, 0, 0, &ex);
|
||||
if (!jsResult && ex) {
|
||||
jsc_printError(context, ex, scriptPath);
|
||||
failed = 1;
|
||||
}
|
||||
}
|
||||
if (scriptContent != NULL) {
|
||||
free(scriptContent);
|
||||
}
|
||||
JSStringRelease(jsScript);
|
||||
JSGlobalContextRelease(context);
|
||||
globalObject = 0;
|
||||
for(std::vector<HANDLE>::iterator it = loaded_modules.begin();
|
||||
it != loaded_modules.end(); ++it) {
|
||||
HANDLE handle = *it;
|
||||
dlclose(handle);
|
||||
}
|
||||
if (failed) {
|
||||
printf("FAIL: Some tests failed.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<h2 id="typemaps"><a href="#TOC">Typemaps</a></h2>
|
||||
<p>A typemap is nothing more than a code generation rule that is attached to a specific C datatype. For example, to convert integers from javascript to C, you might define a typemap like this:</p>
|
||||
<pre><code><br />%typemap(in) int {
|
||||
$1 = ($1_ltype)JSValueToNumber(context, $input, NULL); %}
|
||||
printf("Received an integer : %d\n",$1);
|
||||
}
|
||||
</code></pre>
|
||||
<p>Typemaps are always associated with some specific aspect of code generation. In this case, the "in" method refers to the conversion of input arguments to C/C++. The datatype int is the datatype to which the typemap will be applied. The supplied C code is used to convert values. In this code a number of special variable prefaced by a $ are used. The $1 variable is placeholder for a local variable of type int.</p>
|
||||
<h2 id="javascript-typemaps"><a href="#TOC">Javascript typemaps</a></h2>
|
||||
<p>The previous section illustrated an "in" typemap for converting javascript objects to C. A variety of different typemap methods are defined by the javascript module. For example, to convert a C integer back into a javascript object, you might define an "out" typemap like this:</p>
|
||||
<pre><code><br />%typemap(out) int {
|
||||
$result = JSValueMakeNumber(context, $1);
|
||||
}
|
||||
</code></pre>
|
||||
<p>The Javascript module makes use of Swig's unified template library.</p>
|
||||
<h2 id="typemap-variables"><a href="#TOC">Typemap variables</a></h2>
|
||||
<p>Within typemap code, a number of special variables prefaced with a $ may appear. A full list of variables can be found in the "Typemaps" chapter. This is a list of the most common variables:</p>
|
||||
<p><code>$1</code>: A C local variable corresponding to the actual type specified in the %typemap directive. For input values, this is a C local variable that's supposed to hold an argument value. For output values, this is the raw result that's supposed to be returned to Javascript.</p>
|
||||
<p><code>$input</code>: A javascript Object * holding a raw javascript object with an argument or variable value.</p>
|
||||
<p><code>$result</code>: A javascript Object * that holds the result to be returned to javascript.</p>
|
||||
<p><code>$1_name</code>: The parameter name that was matched.</p>
|
||||
<p><code>$1_type</code>: The actual C datatype matched by the typemap.</p>
|
||||
<p><code>$1_ltype</code>: An assignable version of the datatype matched by the typemap (a type that can appear on the left-hand-side of a C assignment operation). This type is stripped of qualifiers and may be an altered version of <code>$1_type</code>. All arguments and local variables in wrapper functions are declared using this type so that their values can be properly assigned.</p>
|
||||
<p><code>$symname</code>: The javascript name of the wrapper function being created.</p>
|
||||
<h1 id="javascript-specification-of-a-code-generator-for-jsc"><a href="#TOC">Javascript: Specification of a Code Generator for JSC</a></h1>
|
||||
<p>The module implementation tries to accomplish a separation of logic and code generation by making use of code templates. In the following, the templates are explained.</p>
|
||||
<h1 id="top-level-structure"><a href="#TOC">Top Level structure</a></h1>
|
||||
<p>The generated code consists of the following blocks:</p>
|
||||
<pre><code><br /><RUNTIME>
|
||||
<INCLUDES>
|
||||
<HELPER_FUNCTIONS>
|
||||
<FUNCTION_WRAPPERS>
|
||||
<INITIALIZER>
|
||||
</code></pre>
|
||||
<ul>
|
||||
<li><code>RUNTIME</code>: runtime code generated by swig</li>
|
||||
<li><code>HELPER_FUNCTIONS</code>: static, from swg-file</li>
|
||||
<li><code>INCLUDES</code>: static, module property</li>
|
||||
<li><code>FUNCTION_WRAPPERS</code>: dynamically growing, on method declarations</li>
|
||||
<li><code>INITIALIZER</code>: dynamically growing, aggregates everything</li>
|
||||
</ul>
|
||||
<h2 id="includes"><a href="#TOC">INCLUDES</a></h2>
|
||||
<pre><code><br />#include <JavaScriptCore/JavaScript.h>
|
||||
<USER_DEFINED_INCLUDES>
|
||||
</code></pre>
|
||||
<p><code>USER_DEFINED_INCLUDES</code>: a module property</p>
|
||||
<h2 id="helper_functions"><a href="#TOC"><code>HELPER_FUNCTIONS</code></a></h2>
|
||||
<p>A lot of boiler-plate code can be shifted into static helper functions:</p>
|
||||
<pre><code><br />bool JS_registerClass(JSGlobalContextRef& context, JSObjectRef& parentObject,const char* className,
|
||||
JSClassDefinition* definition) {
|
||||
JSStringRef js_className = JSStringCreateWithUTF8CString(className);
|
||||
JSObjectRef classObject = JSObjectMake(context, JSClassCreate(definition), NULL);
|
||||
JSObjectSetProperty(context, parentObject,js_className, classObject,kJSPropertyAttributeNone, NULL);
|
||||
JSStringRelease(js_className);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JS_registerNamespace(JSGlobalContextRef& context,JSObjectRef& namespaceObj,JSObjectRef& parentNamespace,const char* name)
|
||||
{
|
||||
JSStringRef js_name = JSStringCreateWithUTF8CString(name);
|
||||
JSObjectSetProperty(context, parentNamespace,js_name, namespaceObj,kJSPropertyAttributeNone, NULL);
|
||||
JSStringRelease(js_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JS_registerFunction(JSGlobalContextRef context, JSObjectRef object, const char* functionName, JSObjectCallAsFunctionCallback callback)
|
||||
{
|
||||
JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName);
|
||||
JSObjectSetProperty(context,object,js_functionName,JSObjectMakeFunctionWithCallback(context,
|
||||
js_functionName, callback), kJSPropertyAttributeNone, NULL);
|
||||
JSStringRelease(js_functionName);
|
||||
return true;
|
||||
}
|
||||
bool JS_veto_set_variable(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
||||
{
|
||||
char buffer[256];
|
||||
char msg[512];
|
||||
int res;
|
||||
JSStringGetUTF8CString(propertyName, buffer, 256);
|
||||
res = sprintf(msg, "Tried to write read-only variable: %s.", buffer);
|
||||
if(res<0) {
|
||||
SWIG_exception(SWIG_ERROR, "Tried to write read-only variable.");
|
||||
} else {
|
||||
SWIG_exception(SWIG_ERROR, msg);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
JSValueRef JS_CharPtrToJSValue(JSContextRef context, char* cstr) {
|
||||
JSValueRef val;
|
||||
JSStringRef jsstring = JSStringCreateWithUTF8CString((char*) cstr);
|
||||
val = JSValueMakeString(context, jsstring);
|
||||
JSStringRelease(jsstring);
|
||||
return val;
|
||||
}
|
||||
</code></pre>
|
||||
<h2 id="function_wrappers"><a href="#TOC"><code>FUNCTION_WRAPPERS</code></a></h2>
|
||||
<p>There are different types of function wrappers: - Member Functions - Getproperty / Setproperty - Global Functions (global/namespace/class) - Constructors / Destructors</p>
|
||||
<h2 id="member-functions"><a href="#TOC">Member Functions</a></h2>
|
||||
<pre><code><br />JSValueRef ${functionname}(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
||||
{
|
||||
${LOCALS}
|
||||
${CODE}
|
||||
return jsresult;
|
||||
|
||||
goto fail;
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
</code></pre>
|
||||
<ul>
|
||||
<li><code>functionname</code>: the name of generated wrapper for function</li>
|
||||
<li><code>LOCALS</code>: declarations for input arguments</li>
|
||||
<li><code>CODE</code>: contains input marshalling, the action, and output marshalling</li>
|
||||
</ul>
|
||||
<h2 id="setproperty"><a href="#TOC">Setproperty</a></h2>
|
||||
<pre><code><br />bool ${setname}(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
||||
{
|
||||
${LOCALS}
|
||||
${CODE}
|
||||
return jsresult;
|
||||
|
||||
goto fail;
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
</code></pre>
|
||||
<ul>
|
||||
<li><code>setname</code>: the name of the generated wrapper for setproperty.</li>
|
||||
<li><code>LOCALS</code>: declarations for input arguments</li>
|
||||
<li><code>CODE</code>: contains input marshalling, and the action</li>
|
||||
</ul>
|
||||
<h2 id="getproperty"><a href="#TOC">Getproperty</a></h2>
|
||||
<pre><code><br />JSValueRef ${getname}(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
|
||||
{
|
||||
${LOCALS}
|
||||
${CODE}
|
||||
return jsresult;
|
||||
|
||||
goto fail;
|
||||
fail:
|
||||
return NULL;
|
||||
}
|
||||
</code></pre>
|
||||
<ul>
|
||||
<li><code>getname</code>: the name of the generated wrapper for the getproperty</li>
|
||||
<li><code>LOCALS</code>: declarations for output arguments</li>
|
||||
<li><code>CODE</code>: contains the action, and output marshalling</li>
|
||||
</ul>
|
||||
<h2 id="global-functions"><a href="#TOC">Global Functions</a></h2>
|
||||
<pre><code><br />JSStaticValue ${namespace}_values[] = {
|
||||
${jsglobalvariables}
|
||||
{ 0, 0, 0, 0 }
|
||||
};
|
||||
JSStaticFunction ${namespace}_functions[] = {
|
||||
${jsglobalfunctions}
|
||||
{ 0, 0, 0 }
|
||||
};
|
||||
JSClassDefinition ${namespace}_classDefinition;
|
||||
</code></pre>
|
||||
<h2 id="variable-declaration"><a href="#TOC">Variable declaration</a></h2>
|
||||
<pre><code><br />{"${propertyname}",${getname}, ${setname}, kJSPropertyAttributeNone}
|
||||
</code></pre>
|
||||
<p>This is used to fill variable definition tables. <code>kJSPropertyAttributeNone</code> is JSC specific and means that the variable has a getter and setter. Even for read-only variables a setter is used which throws an exception.</p>
|
||||
<h2 id="constructor"><a href="#TOC">Constructor</a></h2>
|
||||
<p>~<sub>~</sub></p>
|
||||
<p>JSObjectRef <em>wrap_create</em><span class="math"><em>c</em><em>l</em><em>a</em><em>s</em><em>s</em><em>n</em><em>a</em><em>m</em><em>e</em><sub><em>m</em></sub><em>a</em><em>n</em><em>g</em><em>l</em><em>e</em><em>d</em></span>{overloadext}(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) { ${LOCALS} <span class="math">${CODE} return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$</span>{type_mangled}, SWIG_POINTER_OWN);</p>
|
||||
<pre><code>goto fail;
|
||||
fail:
|
||||
return NULL;
|
||||
</code></pre>
|
||||
<p>}</p>
|
||||
<pre><code>- `classname_mangled` is the mangled qualified class name, e.g., `foo::A -> foo_A`
|
||||
- `LOCALS`: declarations for input arguments
|
||||
- `CODE`: contains input marshalling, and the action
|
||||
|
||||
## Destructors
|
||||
</code></pre>
|
||||
<p>void <em>wrap</em><span class="math">${classname_mangled}_finalize(JSObjectRef thisObject) { SWIG_PRV_DATA* t = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject); if(t && t->swigCMemOwn) delete ($</span>{type}*)(t->swigCObject); if(t) delete t; }</p>
|
||||
<pre><code><br />- `classname_mangled` is the mangled qualified class name, e.g., `foo::A -> foo_A`
|
||||
|
||||
## Initializer
|
||||
</code></pre>
|
||||
<p>bool ${modulename}_initialize(JSGlobalContextRef context) { SWIG_InitializeModule(0);</p>
|
||||
<pre><code>JSObjectRef global_object = JSContextGetGlobalObject(context);
|
||||
|
||||
/* Initialize the base swig type object */
|
||||
_SwigObject_objectDefinition.staticFunctions = _SwigObject_functions;
|
||||
_SwigObject_objectDefinition.staticValues = _SwigObject_values;
|
||||
_SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition);
|
||||
|
||||
/* Create objects for namespaces */
|
||||
${create_namespaces}
|
||||
|
||||
/* Create classes */
|
||||
${initializercode}
|
||||
|
||||
/* Register namespaces */
|
||||
${register_namespaces}
|
||||
|
||||
return true;
|
||||
</code></pre>
|
||||
<p>}</p>
|
||||
<pre><code><br />## Class template defintions
|
||||
|
||||
A class is specified by a static part (`*_classDefinition`) and a dynamic part (`*_objectDefinition`).
|
||||
</code></pre>
|
||||
<p>${classname_mangled}_classDefinition.staticFunctions = ${classname_mangled}_staticFunctions; ${classname_mangled}_classDefinition.staticValues = ${classname_mangled}_staticValues; <span class="math">${classname_mangled}_classDefinition.callAsConstructor = _wrap_create_$</span>{classname_mangled}; ${classname_mangled}_objectDefinition.staticValues = ${classname_mangled}_values; ${classname_mangled}_objectDefinition.staticFunctions = ${classname_mangled}_functions; ${classname_mangled}_objectDefinition.parentClass = ${base_classname}_classRef; JSClassRef <span class="math">${classname_mangled}_classRef = JSClassCreate(&$</span>{classname_mangled}<em>objectDefinition); SWIGTYPE</em>${classtype_mangled}->clientdata = ${classname_mangled}_classRef;%}</p>
|
||||
<pre><code><br />Notes:
|
||||
- `classname_mangled` is the mangled qualified class name, e.g., `foo::A -> foo_A`
|
||||
which is retrieved by `Swig_name_mangle(Getattr(n, "name"))`
|
||||
- ClassDefinitions are built using the staticValues array and the staticFunction array. The staticValues and staticFunctions arrays are the simplest and most efficient means for vending custom properties to the class object.
|
||||
|
||||
## Inheritance
|
||||
</code></pre>
|
||||
<p>{${classname_mangled}_objectDefinition.parentClass = ${base_classname}_classRef};</p>
|
||||
<pre><code><br />- `classname_mangled` is the mangled qualified class name, e.g., `foo::A -> foo_A`
|
||||
- Note: multiple inheritance is not possible; thus we will always take the first parent class
|
||||
|
||||
## Namespaces
|
||||
|
||||
Namespaces are objects without class templates. i.e., instances are created, referenced locally, used as contexts for other registrations, and stored in the according parent contexts.
|
||||
</code></pre>
|
||||
<p>${namespace}_classDefinition.staticFunctions = ${namespace}_functions; ${namespace}_classDefinition.staticValues = ${namespace}_values; JSObjectRef <span class="math">${namespace}_object = JSObjectMake(context, JSClassCreate(&$</span>{namespace}_classDefinition), NULL);</p>
|
||||
<pre><code><br />## Registration
|
||||
|
||||
The registration part consists of registering classes at contexts (i.e., global or namespace), methods and properties at classes or contexts, and namespaces as objects at parent contexts.
|
||||
|
||||
* Global functions
|
||||
</code></pre>
|
||||
<p>JS_registerFunction(${context}, <span class="math">${context_object}, "$</span>{functionname}", ${functionwrapper}</p>
|
||||
<pre><code><br />* Classes
|
||||
</code></pre>
|
||||
<p>JS_registerClass(context, <span class="math">${namespace}_object, "$</span>{classname}", &${classname_mangled}_classDefinition)</p>
|
||||
<pre><code><br />Note: every class template has an associated constructor function wrapper, which is registered here
|
||||
|
||||
* Namespaces
|
||||
</code></pre>
|
||||
<p>${namespace}_classDefinition.staticFunctions = ${namespace}_functions; ${namespace}_classDefinition.staticValues = ${namespace}_values; JSObjectRef <span class="math">${namespace}_object = JSObjectMake(context, JSClassCreate(&$</span>{namespace}_classDefinition), NULL);</p>
|
||||
<pre><code><br />Namespaces are registered using:
|
||||
</code></pre>
|
||||
<p>JS_registerNamespace(context, ${namespace}_object, <span class="math">${parent_namespace}_object, "$</span>{namespace}");</p>
|
||||
<p>~<sub>~</sub></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
1103
Doc/Manual/Javascript.md
Normal file
1103
Doc/Manual/Javascript.md
Normal file
File diff suppressed because it is too large
Load diff
56
Doc/Manual/pandoc_template.html
Normal file
56
Doc/Manual/pandoc_template.html
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"$if(lang)$ lang="$lang$" xml:lang="$lang$"$endif$>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="Content-Style-Type" content="text/css" />
|
||||
<meta name="generator" content="pandoc" />
|
||||
$for(author-meta)$
|
||||
<meta name="author" content="$author-meta$" />
|
||||
$endfor$
|
||||
$if(date-meta)$
|
||||
<meta name="date" content="$date-meta$" />
|
||||
$endif$
|
||||
<title>$if(title-prefix)$$title-prefix$ - $endif$$if(pagetitle)$$pagetitle$$endif$</title>
|
||||
$if(highlighting-css)$
|
||||
<style type="text/css">
|
||||
$highlighting-css$
|
||||
</style>
|
||||
$endif$
|
||||
$for(css)$
|
||||
<link rel="stylesheet" href="$css$" $if(html5)$$else$type="text/css" $endif$/>
|
||||
$endfor$
|
||||
$if(math)$
|
||||
$math$
|
||||
$endif$
|
||||
$for(header-includes)$
|
||||
$header-includes$
|
||||
$endfor$
|
||||
</head>
|
||||
<body>
|
||||
$for(include-before)$
|
||||
$include-before$
|
||||
$endfor$
|
||||
$if(title)$
|
||||
<div id="$idprefix$header">
|
||||
<h1 class="title">$title$</h1>
|
||||
$for(author)$
|
||||
<h2 class="author">$author$</h2>
|
||||
$endfor$
|
||||
$if(date)$
|
||||
<h3 class="date">$date$</h3>
|
||||
$endif$
|
||||
</div>
|
||||
$endif$
|
||||
$if(toc)$
|
||||
<div class="sectiontoc">
|
||||
$toc$
|
||||
</div>
|
||||
$endif$
|
||||
<div>
|
||||
$body$
|
||||
</div>
|
||||
$for(include-after)$
|
||||
$include-after$
|
||||
$endfor$
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -25,7 +25,7 @@ div.indent {
|
|||
margin-right: 4em;
|
||||
}
|
||||
|
||||
div.code {
|
||||
div.code, div:not(.code) pre {
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
padding: 2pt;
|
||||
|
|
@ -82,3 +82,7 @@ div.indent p {
|
|||
margin-right: 0;
|
||||
}
|
||||
|
||||
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
|
||||
text-decoration:none;
|
||||
color: #000000;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -513,6 +513,65 @@ java_clean:
|
|||
rm -f core @EXTRA_CLEAN@
|
||||
rm -f *.@OBJEXT@ *@JAVASO@
|
||||
|
||||
##################################################################
|
||||
##### JAVASCRIPT ######
|
||||
##################################################################
|
||||
|
||||
# You need to set this variable to the jscore[or other javascript engine] directories containing the
|
||||
# files "JavaScript.h" and others
|
||||
JS_INCLUDE= @JSCOREINC@
|
||||
|
||||
# Extra JAVASCRIPT specific dynamic linking options
|
||||
JS_DLNK = @JSCOREDYNAMICLINKING@
|
||||
JS_LIBPREFIX = @JSCORELIBRARYPREFIX@
|
||||
JSSO =@JSCORESO@
|
||||
JSLDSHARED = @JSCORELDSHARED@
|
||||
JSCXXSHARED = @JSCORECXXSHARED@
|
||||
JSCFLAGS = @JSCORECFLAGS@
|
||||
JSCXXFLAGS = @JSCXXFLAGS@
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Build a javascript dynamically loadable module (C)
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
javascript: $(SRCS)
|
||||
$(SWIG) -javascript -jsc -debug-templates $(SWIGOPT) $(INTERFACEPATH)
|
||||
$(CC) -c $(CCSHARED) $(JSCFLAGS) $(SRCS) $(ISRCS) $(INCLUDES) $(JS_INCLUDE)
|
||||
$(JSLDSHARED) $(CCSHARED) $(JSCFLAGS) $(OBJS) $(IOBJS) $(JS_DLNK) $(LIBS) -o $(JS_LIBPREFIX)$(TARGET)$(JSSO)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Build a javascript dynamically loadable module (C++)
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
javascript_cpp: $(SRCS)
|
||||
$(SWIG) -javascript -jsc -c++ -debug-templates $(SWIGOPT) $(INTERFACEPATH)
|
||||
$(CXX) -c $(CCSHARED) $(JSCXXFLAGS) $(SRCS) $(CXXSRCS) $(ICXXSRCS) $(INCLUDES) $(JS_INCLUDE)
|
||||
$(JSCXXSHARED) $(CCSHARED) $(OBJS) $(IOBJS) $(JS_DLNK) $(LIBS) $(CPP_DLLIBS) -o $(JS_LIBPREFIX)$(TARGET)$(JSSO)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Compile a javascript executable
|
||||
# ----------------------------------------------------------------
|
||||
javascript_exe: $(SRCS)
|
||||
$(CXX) $(CXXFLAGS) $(JS_INCLUDE) $(JSCXXSRCS) $(LIBS) $(JS_DLNK) -o $(JAVASCRIPT_EXE)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Run the Compile a javascript executable
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
javascript_run: $(SRCS)
|
||||
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(JAVASCRIPT_EXE) -l $(JAVASCRIPT_MODULE) $(JS_SCRIPT)
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Cleaning the javascript examples
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
javascript_clean:
|
||||
rm -f *_wrap* runme
|
||||
rm -f core @EXTRA_CLEAN@
|
||||
rm -f *.@OBJEXT@ *@JSCORESO@
|
||||
|
||||
|
||||
|
||||
##################################################################
|
||||
##### MODULA3 ######
|
||||
##################################################################
|
||||
|
|
|
|||
24
Examples/javascript/class/Makefile
Executable file
24
Examples/javascript/class/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
28
Examples/javascript/class/example.cpp
Executable file
28
Examples/javascript/class/example.cpp
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;
|
||||
}
|
||||
35
Examples/javascript/class/example.h
Executable file
35
Examples/javascript/class/example.h
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
/* 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 void printMe(void);
|
||||
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"
|
||||
|
||||
51
Examples/javascript/class/runme.js
Executable file
51
Examples/javascript/class/runme.js
Executable file
|
|
@ -0,0 +1,51 @@
|
|||
// file: runme.js
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
print("Creating some objects:");
|
||||
c = new example.Circle(10);
|
||||
print("Created circle " + c);
|
||||
s = new example.Square(10);
|
||||
print("Created square " + s);
|
||||
|
||||
// ----- Access a static member -----
|
||||
print("\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;
|
||||
|
||||
print("\nHere is their current position:");
|
||||
print("Circle = (" + c.x + "," + c.y + ")");
|
||||
print("Square = (" + s.x + "," + s.y + ")");
|
||||
|
||||
print("\nHere is their new position:");
|
||||
print("Circle = (" + c.x + "," + c.y + ")");
|
||||
print("Square = (" + s.x + "," + s.y + ")");
|
||||
|
||||
|
||||
// ----- Call some methods -----
|
||||
print("\nHere are some properties of the shapes:");
|
||||
print("Circle:");
|
||||
print("area = " + c.area() + "");
|
||||
print("perimeter = " + c.perimeter() + "");
|
||||
print("\n");
|
||||
print("Square:");
|
||||
print("area = " + s.area() + "");
|
||||
print("perimeter = " + s.perimeter() + "");
|
||||
|
||||
// ----- Delete everything -----
|
||||
print("\nGuess I'll clean up now");
|
||||
// Note: this invokes the virtual destructor
|
||||
delete c;
|
||||
delete s;
|
||||
|
||||
print (example.Shape.nshapes + " shapes remain");
|
||||
|
||||
print("Goodbye");
|
||||
24
Examples/javascript/constant/Makefile
Executable file
24
Examples/javascript/constant/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS =
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
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;
|
||||
14
Examples/javascript/constant/runme.js
Executable file
14
Examples/javascript/constant/runme.js
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
// file: runme.js
|
||||
|
||||
print("ICONST = " + example.ICONST + " (should be 42)\n");
|
||||
print("FCONST = " + example.FCONST + " (should be 2.1828)\n");
|
||||
print("CCONST = " + example.CCONST + " (should be 'x')\n");
|
||||
print("CCONST2 = " + example.CCONST2 + " (this should be on a new line)\n");
|
||||
print("SCONST = " + example.SCONST + " (should be 'Hello World')\n");
|
||||
print("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')\n");
|
||||
print("EXPR = " + example.EXPR + " (should be 48.5484)\n");
|
||||
print("iconst = " + example.iconst + " (should be 37)\n");
|
||||
print("fconst = " + example.fconst + " (should be 3.14)\n");
|
||||
|
||||
print("EXTERN = " + example.EXTERN + " (should be undefined)\n");
|
||||
print("FOO = " + example.FOO + " (should be undefined)\n");
|
||||
24
Examples/javascript/enum/Makefile
Executable file
24
Examples/javascript/enum/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
37
Examples/javascript/enum/example.cpp
Executable file
37
Examples/javascript/enum/example.cpp
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"
|
||||
|
||||
34
Examples/javascript/enum/runme.js
Executable file
34
Examples/javascript/enum/runme.js
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
// file: runme.py
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
// Print out the value of some enums
|
||||
print("*** color ***");
|
||||
print(" RED =" + example.RED);
|
||||
print(" BLUE =" + example.BLUE);
|
||||
print(" GREEN =" + example.GREEN);
|
||||
|
||||
print("\n*** Foo::speed ***");
|
||||
print(" Foo_IMPULSE =" + example.Foo.IMPULSE);
|
||||
print(" Foo_WARP =" + example.Foo.WARP);
|
||||
print(" Foo_LUDICROUS =" + example.Foo.LUDICROUS);
|
||||
|
||||
print("\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);
|
||||
|
||||
print("\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
|
||||
print("example.BLUE= " + example.BLUE);
|
||||
|
||||
// enum value LUDICROUS of enum Foo::speed is accessed as as property of cconst
|
||||
print("example.speed.LUDICROUS= " + example.Foo.LUDICROUS);
|
||||
24
Examples/javascript/exception/Makefile
Executable file
24
Examples/javascript/exception/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
1
Examples/javascript/exception/example.cpp
Normal file
1
Examples/javascript/exception/example.cpp
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"
|
||||
|
||||
54
Examples/javascript/exception/runme.js
Normal file
54
Examples/javascript/exception/runme.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
//file: runme.js
|
||||
// Throw a lot of exceptions
|
||||
|
||||
t = new example.Test();
|
||||
try{
|
||||
t.unknown();
|
||||
throw -1;
|
||||
} catch(error)
|
||||
{
|
||||
if(error == -1) {
|
||||
print("t.unknown() didn't throw");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.simple();
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.simple() did not throw");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.message();
|
||||
throw -1;
|
||||
} catch(error){
|
||||
if(error == -1) {
|
||||
print("t.message() did not throw");
|
||||
}
|
||||
}
|
||||
|
||||
try{
|
||||
t.hosed();
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.hosed() did not throw");
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=1; i<4; i++) {
|
||||
try{
|
||||
t.multi(i);
|
||||
throw -1;
|
||||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.mulit(" + i + ") did not throw");
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Examples/javascript/functor/Makefile
Executable file
24
Examples/javascript/functor/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
0
Examples/javascript/functor/example.cpp
Normal file
0
Examples/javascript/functor/example.cpp
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>;
|
||||
|
||||
|
||||
|
||||
|
||||
16
Examples/javascript/functor/runme.js
Normal file
16
Examples/javascript/functor/runme.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Operator overloading 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
|
||||
|
||||
print(a.result());
|
||||
print(b.result());
|
||||
|
||||
24
Examples/javascript/namespace/Makefile
Executable file
24
Examples/javascript/namespace/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
36
Examples/javascript/namespace/example.cpp
Normal file
36
Examples/javascript/namespace/example.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include <iostream>
|
||||
#include "example.h"
|
||||
|
||||
#define M_PI 3.14159
|
||||
|
||||
|
||||
/* A global variable */
|
||||
namespace nspace
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Circle::Circle(): radius(1.0) {}
|
||||
|
||||
Circle::Circle(double r): radius(r) {
|
||||
std::cout << "created Circle with r=" << radius << std::endl;
|
||||
}
|
||||
|
||||
double Circle::area() {
|
||||
std::cout << "Circle::area called, r=" << radius << std::endl;
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
}
|
||||
20
Examples/javascript/namespace/example.h
Normal file
20
Examples/javascript/namespace/example.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
namespace nspace {
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
|
||||
class Circle
|
||||
{
|
||||
public:
|
||||
Circle();
|
||||
|
||||
Circle(double r);
|
||||
|
||||
double area();
|
||||
|
||||
double radius;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
10
Examples/javascript/namespace/example.i
Normal file
10
Examples/javascript/namespace/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%feature("nspace", 1);
|
||||
|
||||
%include "example.h"
|
||||
11
Examples/javascript/namespace/runme.js
Normal file
11
Examples/javascript/namespace/runme.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
print("Global variable Foo=" + example.nspace.Foo);
|
||||
example.nspace.Foo = 5;
|
||||
print("Variable Foo changed to " + example.nspace.Foo);
|
||||
print("GCD of number 6,18 is " + example.nspace.gcd(6,18));
|
||||
|
||||
print("Creating some objects:");
|
||||
c = new example.nspace.Circle(10);
|
||||
print("area = " + c.area());
|
||||
|
||||
|
||||
|
||||
24
Examples/javascript/operator/Makefile
Executable file
24
Examples/javascript/operator/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
0
Examples/javascript/operator/example.cpp
Normal file
0
Examples/javascript/operator/example.cpp
Normal file
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);
|
||||
}
|
||||
};
|
||||
|
||||
25
Examples/javascript/operator/runme.js
Normal file
25
Examples/javascript/operator/runme.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Operator overloading example
|
||||
|
||||
a = new example.Complex(2,3);
|
||||
b = new example.Complex(-5,10);
|
||||
|
||||
print ("a =" + a);
|
||||
print ("b =" + b);
|
||||
|
||||
c = a.plus(b);
|
||||
|
||||
print("c =" + c);
|
||||
print("a*b =" + a.times(b));
|
||||
print("a-c =" + a.minus(c));
|
||||
|
||||
e = example.Complex.copy(a.minus(c));
|
||||
print("e =" + e);
|
||||
|
||||
// Big expression
|
||||
f = a.plus(b).times(c.plus(b.times(e))).plus(a.uminus());
|
||||
print("f =" + f);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
24
Examples/javascript/overload/Makefile
Executable file
24
Examples/javascript/overload/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
0
Examples/javascript/overload/example.cpp
Normal file
0
Examples/javascript/overload/example.cpp
Normal file
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;
|
||||
}
|
||||
20
Examples/javascript/overload/example.i
Normal file
20
Examples/javascript/overload/example.i
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* 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_string) f(const char* s);
|
||||
%rename(f_bool) f(bool val);
|
||||
%rename(f_long) f(long val);
|
||||
%rename(f_double) f(double val);
|
||||
|
||||
%include "example.h"
|
||||
7
Examples/javascript/overload/runme.js
Normal file
7
Examples/javascript/overload/runme.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
example.f();
|
||||
example.f(1);
|
||||
example.f(1, 2);
|
||||
example.f_string("bla");
|
||||
example.f_bool(false);
|
||||
example.f_long(11111111111);
|
||||
example.f_double(1.0);
|
||||
24
Examples/javascript/pointer/Makefile
Executable file
24
Examples/javascript/pointer/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
16
Examples/javascript/pointer/example.cpp
Executable file
16
Examples/javascript/pointer/example.cpp
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);
|
||||
|
||||
|
||||
|
||||
|
||||
38
Examples/javascript/pointer/runme.js
Executable file
38
Examples/javascript/pointer/runme.js
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
// file: runme.js
|
||||
|
||||
// First create some objects using the pointer library.
|
||||
print("Testing the pointer library\n");
|
||||
a = example.new_intp();
|
||||
b = example.new_intp();
|
||||
c = example.new_intp();
|
||||
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
print(" a = " + example.intp_value(a) + "\n");
|
||||
print(" b = " + example.intp_value(b) + "\n");
|
||||
print(" c = " + example.intp_value(c) + "\n");
|
||||
|
||||
//// Call the add() function with some pointers
|
||||
example.add(a, b, c);
|
||||
|
||||
//
|
||||
//// Now get the result
|
||||
r = example.intp_value(c);
|
||||
print(" 37 + 42 = " + r + "\n");
|
||||
|
||||
// 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
|
||||
|
||||
//print("Trying the typemap library");
|
||||
//r = example.subtract(37,42);
|
||||
//print("37 - 42 =" + r);
|
||||
|
||||
|
||||
24
Examples/javascript/reference/Makefile
Executable file
24
Examples/javascript/reference/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
46
Examples/javascript/reference/example.cpp
Executable file
46
Examples/javascript/reference/example.cpp
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;
|
||||
}
|
||||
}
|
||||
};
|
||||
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.
|
||||
// TODO: deleteion of vector objects created here
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
print("Creating some objects:\n");
|
||||
a = new example.Vector(3,4,5);
|
||||
b = new example.Vector(10,11,12);
|
||||
|
||||
print(" created" + a.print());
|
||||
print(" 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.
|
||||
|
||||
print("Adding a+b\n");
|
||||
c = example.addv(a, b);
|
||||
print("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
|
||||
print("Creating an array of vectors\n");
|
||||
va = new example.VectorArray(10);
|
||||
print("va = " + va + "\n");
|
||||
|
||||
// ----- 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
|
||||
|
||||
print("Getting some array values\n");
|
||||
for (i = 0; i < 5; i++) {
|
||||
temp = va.get(i);
|
||||
print(i,temp.print());
|
||||
}
|
||||
|
||||
// Watch under resource meter to check on this
|
||||
print("Making sure we don't leak memory.\n");
|
||||
for (i = 0; i < 1000000; i++) {
|
||||
c = va.get(i % 10);
|
||||
}
|
||||
//---------TODO---------
|
||||
//----- Clean up -----
|
||||
//print("Cleaning up\n");
|
||||
|
||||
//example.delete_VectorArray(va);
|
||||
//example.delete_Vector(a);
|
||||
//example.delete_Vector(b);
|
||||
|
||||
24
Examples/javascript/simple/Makefile
Executable file
24
Examples/javascript/simple/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
28
Examples/javascript/simple/example.cpp
Executable file
28
Examples/javascript/simple/example.cpp
Executable file
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
15
Examples/javascript/simple/example.i
Executable file
15
Examples/javascript/simple/example.i
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%include "std_string.i"
|
||||
|
||||
%inline %{
|
||||
extern int gcd(int x, int y);
|
||||
extern float gcd(float x, float y);
|
||||
extern char* helloString(char* s);
|
||||
extern void delete_helloString(char *newstr);
|
||||
extern std::string helloString(std::string s);
|
||||
extern void bar(int x, int y = 3, int z = 4);
|
||||
|
||||
extern double Foo;
|
||||
%}
|
||||
26
Examples/javascript/simple/runme.js
Executable file
26
Examples/javascript/simple/runme.js
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
/* file: runme.js */
|
||||
|
||||
/* Call our gcd() function */
|
||||
|
||||
x = 42;
|
||||
y = 105;
|
||||
g = example.gcd(x,y);
|
||||
print("GCD of x and y is=" + g);
|
||||
|
||||
/* Manipulate the Foo global variable */
|
||||
|
||||
/* Output its current value */
|
||||
print("Global variable Foo=" + example.Foo);
|
||||
|
||||
/* Change its value */
|
||||
example.Foo = 3.1415926;
|
||||
|
||||
/* See if the change took effect */
|
||||
print("Variable Foo changed to=" + example.Foo);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
24
Examples/javascript/template/Makefile
Executable file
24
Examples/javascript/template/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
0
Examples/javascript/template/example.cpp
Normal file
0
Examples/javascript/template/example.cpp
Normal file
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>;
|
||||
|
||||
32
Examples/javascript/template/runme.js
Normal file
32
Examples/javascript/template/runme.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// file: runme.js
|
||||
|
||||
//Call some templated functions
|
||||
print(example.maxint(3,7));
|
||||
print(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);
|
||||
|
||||
print(sum);
|
||||
|
||||
sum = 0.0;
|
||||
for(i=0;i<=1000;i++)
|
||||
sum = sum + dv.getitem(i);
|
||||
print(sum);
|
||||
|
||||
delete iv;
|
||||
delete dv;
|
||||
|
||||
|
||||
24
Examples/javascript/variables/Makefile
Executable file
24
Examples/javascript/variables/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
91
Examples/javascript/variables/example.c
Executable file
91
Examples/javascript/variables/example.c
Executable file
|
|
@ -0,0 +1,91 @@
|
|||
/* 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;
|
||||
const char cstrvar[] = "Goodbye";
|
||||
int *iptrvar = 0;
|
||||
char name[256] = "Dave";
|
||||
char path[256] = "/home/beazley";
|
||||
|
||||
|
||||
/* Global variables involving a structure */
|
||||
Point *ptptr = 0;
|
||||
Point pt = { 10, 20 };
|
||||
|
||||
/* A variable that we will make read-only in the interface */
|
||||
int status = 1;
|
||||
|
||||
/* A debugging function to print out their values */
|
||||
|
||||
void print_vars() {
|
||||
printf("ivar = %d\n", ivar);
|
||||
printf("svar = %d\n", svar);
|
||||
printf("lvar = %ld\n", lvar);
|
||||
printf("uivar = %u\n", uivar);
|
||||
printf("usvar = %u\n", usvar);
|
||||
printf("ulvar = %lu\n", ulvar);
|
||||
printf("scvar = %d\n", scvar);
|
||||
printf("ucvar = %u\n", ucvar);
|
||||
printf("fvar = %g\n", fvar);
|
||||
printf("dvar = %g\n", dvar);
|
||||
printf("cvar = %c\n", cvar);
|
||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||
printf("iptrvar = %p\n", iptrvar);
|
||||
printf("name = %s\n", name);
|
||||
printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
|
||||
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||
printf("status = %d\n", status);
|
||||
}
|
||||
|
||||
/* A function to create an integer (to test iptrvar) */
|
||||
|
||||
int *new_int(int value) {
|
||||
int *ip = (int *) malloc(sizeof(int));
|
||||
*ip = value;
|
||||
return ip;
|
||||
}
|
||||
|
||||
/* A function to create a point */
|
||||
|
||||
Point *new_Point(int x, int y) {
|
||||
Point *p = (Point *) malloc(sizeof(Point));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
char * Point_print(Point *p) {
|
||||
static char buffer[256];
|
||||
if (p) {
|
||||
sprintf(buffer,"(%d,%d)", p->x,p->y);
|
||||
} else {
|
||||
sprintf(buffer,"null");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void pt_print() {
|
||||
printf("(%d, %d)\n", pt.x, pt.y);
|
||||
}
|
||||
6
Examples/javascript/variables/example.h
Executable file
6
Examples/javascript/variables/example.h
Executable file
|
|
@ -0,0 +1,6 @@
|
|||
/* File: example.h */
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
} Point;
|
||||
|
||||
49
Examples/javascript/variables/example.i
Executable file
49
Examples/javascript/variables/example.i
Executable file
|
|
@ -0,0 +1,49 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Some global variable declarations */
|
||||
%inline %{
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
extern unsigned int uivar;
|
||||
extern unsigned short usvar;
|
||||
extern unsigned long ulvar;
|
||||
extern signed char scvar;
|
||||
extern unsigned char ucvar;
|
||||
extern char cvar;
|
||||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
extern const char cstrvar[];
|
||||
extern int *iptrvar;
|
||||
extern char name[256];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
%}
|
||||
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
|
||||
%inline %{
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%}
|
||||
|
||||
%mutable;
|
||||
|
||||
/* Some helper functions to make it easier to test */
|
||||
%inline %{
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
%}
|
||||
|
||||
68
Examples/javascript/variables/runme.js
Executable file
68
Examples/javascript/variables/runme.js
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
// file: runme.js
|
||||
|
||||
// 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 print out the values of the variables
|
||||
print("Variables (values printed from Python)" + "\n");
|
||||
print("ivar = " + example.ivar + "\n");
|
||||
print("svar = " + example.svar + "\n");
|
||||
print("lvar = " + example.lvar + "\n");
|
||||
print("uivar = " + example.uivar + "\n");
|
||||
print("usvar = " + example.usvar + "\n");
|
||||
print("ulvar = " + example.ulvar + "\n");
|
||||
print("scvar = " + example.scvar + "\n");
|
||||
print("ucvar = " + example.ucvar + "\n");
|
||||
print("fvar = " + example.fvar + "\n");
|
||||
print("dvar = " + example.dvar + "\n");
|
||||
print("cvar = " + example.cvar + "\n");
|
||||
print("strvar = " + example.strvar+ "\n");
|
||||
print("cstrvar = " + example.cstrvar+ "\n");
|
||||
print("iptrvar = " + example.iptrvar+ "\n");
|
||||
print("name = " + example.name + "\n");
|
||||
print("ptptr = " + example.ptptr + ": " + example.Point_print(example.ptptr) + "\n");
|
||||
print("pt = " + example.pt + ": " + example.Point_print(example.pt) + "\n");
|
||||
|
||||
|
||||
print("\nVariables (values printed from C)");
|
||||
|
||||
example.print_vars();
|
||||
|
||||
print("\nNow I'm going to try and modify some read only variables");
|
||||
|
||||
print("Tring to set 'path'");
|
||||
try{
|
||||
example.path = "Whoa!";
|
||||
print("Hey, what's going on?!?! This shouldn't work");
|
||||
}
|
||||
catch(e){
|
||||
print("Good.");
|
||||
}
|
||||
|
||||
print("Trying to set 'status'");
|
||||
try{
|
||||
example.status = 0;
|
||||
print("Hey, what's going on?!?! This shouldn't work");
|
||||
} catch(e){
|
||||
print("Good.");
|
||||
}
|
||||
|
||||
print("\nI'm going to try and update a structure variable.\n");
|
||||
example.pt = example.ptptr;
|
||||
print("The new value is: ");
|
||||
example.pt_print();
|
||||
print("You should see the value: " + example.Point_print(example.ptptr));
|
||||
|
|
@ -95,6 +95,7 @@ C_TEST_BROKEN += \
|
|||
|
||||
|
||||
# C++ test cases. (Can be run individually using: make testcase.cpptest)
|
||||
ifndef SKIP_CPP_CASES
|
||||
CPP_TEST_CASES += \
|
||||
abstract_access \
|
||||
abstract_inherit \
|
||||
|
|
@ -459,6 +460,7 @@ CPP_TEST_CASES += \
|
|||
voidtest \
|
||||
wallkw \
|
||||
wrapmacro
|
||||
endif
|
||||
|
||||
#
|
||||
# Put all the heavy STD/STL cases here, where they can be skipped if needed
|
||||
|
|
@ -488,6 +490,7 @@ endif
|
|||
|
||||
|
||||
# C test cases. (Can be run individually using: make testcase.ctest)
|
||||
ifndef SKIP_C_CASES
|
||||
C_TEST_CASES += \
|
||||
arrays \
|
||||
char_constant \
|
||||
|
|
@ -532,9 +535,10 @@ C_TEST_CASES += \
|
|||
typemap_subst \
|
||||
union_parameter \
|
||||
unions
|
||||
|
||||
endif
|
||||
|
||||
# Multi-module C++ test cases . (Can be run individually using make testcase.multicpptest)
|
||||
ifndef SKIP_MULTI_CPP_CASES
|
||||
MULTI_CPP_TEST_CASES += \
|
||||
clientdata_prop \
|
||||
imports \
|
||||
|
|
@ -543,6 +547,7 @@ MULTI_CPP_TEST_CASES += \
|
|||
mod \
|
||||
template_typedef_import \
|
||||
multi_import
|
||||
endif
|
||||
|
||||
# Custom tests - tests with additional commandline options
|
||||
wallkw.cpptest: SWIGOPT += -Wallkw
|
||||
|
|
|
|||
94
Examples/test-suite/javascript/Makefile.in
Executable file
94
Examples/test-suite/javascript/Makefile.in
Executable file
|
|
@ -0,0 +1,94 @@
|
|||
#######################################################################
|
||||
# Makefile for javascript test-suite
|
||||
#######################################################################
|
||||
|
||||
LANGUAGE = javascript
|
||||
JAVASCRIPT_EXE = ../../../Tools/javascript/javascript
|
||||
JAVASCRIPT_EXE_SRC = ../../../Tools/javascript/javascript.cxx
|
||||
SCRIPTSUFFIX = _runme.js
|
||||
srcdir = @srcdir@
|
||||
top_srcdir = @top_srcdir@
|
||||
top_builddir = @top_builddir@
|
||||
JS_INCLUDE = @JSCOREINC@
|
||||
JS_DLNK = @JSCOREDYNAMICLINKING@
|
||||
JSCXXFLAGS = @JSCXXFLAGS@
|
||||
|
||||
C_TEST_CASES = \
|
||||
preproc \
|
||||
preproc_include
|
||||
|
||||
CPP_TEST_CASES = \
|
||||
abstract_access \
|
||||
abstract_typedef \
|
||||
abstract_typedef2 \
|
||||
abstract_virtual \
|
||||
array_member \
|
||||
arrays_global \
|
||||
char_binary \
|
||||
class_ignore \
|
||||
class_scope_weird \
|
||||
complextest \
|
||||
constover \
|
||||
cpp_enum \
|
||||
cpp_namespace \
|
||||
cpp_static \
|
||||
director_alternating \
|
||||
enum_template \
|
||||
namespace_virtual_method \
|
||||
overload_copy \
|
||||
ret_by_value \
|
||||
struct_value \
|
||||
template_static \
|
||||
typedef_class \
|
||||
typedef_inherit \
|
||||
typedef_scope \
|
||||
typemap_arrays \
|
||||
typemap_delete \
|
||||
typemap_namespace \
|
||||
typemap_ns_using \
|
||||
using1 \
|
||||
using2
|
||||
|
||||
SKIP_CPP_CASES = @SKIP_CPP_CASES@
|
||||
SKIP_C_CASES = @SKIP_C_CASES@
|
||||
SKIP_CPP_STD_CASES = @SKIP_CPP_STD_CASES@
|
||||
SKIP_MULTI_CPP_CASES = @SKIP_MULTI_CPP_CASES@
|
||||
|
||||
include $(srcdir)/../common.mk
|
||||
|
||||
# Overridden variables here
|
||||
|
||||
# Custom tests - tests with additional commandline options
|
||||
|
||||
javascript_exe:
|
||||
$(CXX) $(JSCXXFLAGS) $(JS_INCLUDE) $(JAVASCRIPT_EXE_SRC) $(LIBS) $(JS_DLNK) -o $(JAVASCRIPT_EXE)
|
||||
|
||||
# Rules for the different types of tests
|
||||
%.cpptest: javascript_exe
|
||||
$(setup)
|
||||
+$(swig_and_compile_cpp)
|
||||
$(run_testcase)
|
||||
|
||||
%.ctest: javascript_exe
|
||||
$(setup)
|
||||
+$(swig_and_compile_c)
|
||||
$(run_testcase)
|
||||
|
||||
%.multicpptest: javascript_exe
|
||||
$(setup)
|
||||
+$(swig_and_compile_multi_cpp)
|
||||
$(run_testcase)
|
||||
|
||||
# Runs the testcase. A testcase is only run if
|
||||
# a file is found which has _runme.js appended after the testcase name.
|
||||
run_testcase = \
|
||||
if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then \
|
||||
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) $(JAVASCRIPT_EXE) -l $* $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX); \
|
||||
fi
|
||||
|
||||
# Clean
|
||||
%.clean:
|
||||
|
||||
|
||||
clean:
|
||||
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile javascript_clean
|
||||
4
Examples/test-suite/javascript/README
Executable file
4
Examples/test-suite/javascript/README
Executable file
|
|
@ -0,0 +1,4 @@
|
|||
See ../README for common README file.
|
||||
|
||||
Any testcases which have _runme.js appended after the testcase name will be detected and run.
|
||||
|
||||
5
Examples/test-suite/javascript/abstract_access_runme.js
Normal file
5
Examples/test-suite/javascript/abstract_access_runme.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var d = new abstract_access.D()
|
||||
if (d.do_x() != 1) {
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
var a = new abstract_typedef2.A_UF();
|
||||
|
||||
if (a == undefined)
|
||||
throw "Error";
|
||||
6
Examples/test-suite/javascript/abstract_typedef_runme.js
Normal file
6
Examples/test-suite/javascript/abstract_typedef_runme.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
var e = new abstract_typedef.Engine();
|
||||
var a = new abstract_typedef.A()
|
||||
|
||||
if (a.write(e) != 1) {
|
||||
throw "Error";
|
||||
}
|
||||
9
Examples/test-suite/javascript/abstract_virtual_runme.js
Normal file
9
Examples/test-suite/javascript/abstract_virtual_runme.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
d = new abstract_virtual.D()
|
||||
|
||||
if (d == undefined)
|
||||
throw "Error";
|
||||
|
||||
e = new abstract_virtual.E()
|
||||
|
||||
if (e == undefined)
|
||||
throw "Error";
|
||||
20
Examples/test-suite/javascript/array_member_runme.js
Normal file
20
Examples/test-suite/javascript/array_member_runme.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
var f = new array_member.Foo();
|
||||
f.data = array_member.global_data;
|
||||
|
||||
for (var i=0; i<8; i++) {
|
||||
if (array_member.get_value(f.data,i) != array_member.get_value(array_member.global_data,i)) {
|
||||
throw "Bad array assignment (1)";
|
||||
}
|
||||
}
|
||||
|
||||
for (var i=0; i<8; i++) {
|
||||
array_member.set_value(f.data,i,-i);
|
||||
}
|
||||
|
||||
array_member.global_data = f.data;
|
||||
|
||||
for (var i=0; i<8; i++){
|
||||
if (array_member.get_value(f.data,i) != array_member.get_value(array_member.global_data,i)) {
|
||||
throw "Bad array assignment (2)";
|
||||
}
|
||||
}
|
||||
16
Examples/test-suite/javascript/arrays_global_runme.js
Normal file
16
Examples/test-suite/javascript/arrays_global_runme.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
arrays_global.array_i = arrays_global.array_const_i;
|
||||
|
||||
arrays_global.BeginString_FIX44a;
|
||||
arrays_global.BeginString_FIX44b;
|
||||
arrays_global.BeginString_FIX44c;
|
||||
arrays_global.BeginString_FIX44d;
|
||||
arrays_global.BeginString_FIX44d;
|
||||
arrays_global.BeginString_FIX44b = "12"+'\0'+"45";
|
||||
arrays_global.BeginString_FIX44b;
|
||||
arrays_global.BeginString_FIX44d;
|
||||
arrays_global.BeginString_FIX44e;
|
||||
arrays_global.BeginString_FIX44f;
|
||||
|
||||
arrays_global.test_a("hello","hi","chello","chi");
|
||||
|
||||
arrays_global.test_b("1234567","hi");
|
||||
36
Examples/test-suite/javascript/char_binary_runme.js
Normal file
36
Examples/test-suite/javascript/char_binary_runme.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
var t = new char_binary.Test();
|
||||
if (t.strlen('hile') != 4) {
|
||||
print(t.strlen('hile'));
|
||||
throw("bad multi-arg typemap 1");
|
||||
}
|
||||
|
||||
if (t.strlen('hil\0') != 4) {
|
||||
throw("bad multi-arg typemap 2");
|
||||
}
|
||||
|
||||
/*
|
||||
* creating a raw char*
|
||||
*/
|
||||
var pc = char_binary.new_pchar(5);
|
||||
char_binary.pchar_setitem(pc, 0, 'h');
|
||||
char_binary.pchar_setitem(pc, 1, 'o');
|
||||
char_binary.pchar_setitem(pc, 2, 'l');
|
||||
char_binary.pchar_setitem(pc, 3, 'a');
|
||||
char_binary.pchar_setitem(pc, 4, 0);
|
||||
|
||||
|
||||
if (t.strlen(pc) != 4) {
|
||||
throw("bad multi-arg typemap (3)");
|
||||
}
|
||||
|
||||
char_binary.var_pchar = pc;
|
||||
if (char_binary.var_pchar != "hola") {
|
||||
print(char_binary.var_pchar);
|
||||
throw("bad pointer case (1)");
|
||||
}
|
||||
|
||||
char_binary.var_namet = pc;
|
||||
if (char_binary.var_namet != "hola") {
|
||||
throw("bad pointer case (2)");
|
||||
}
|
||||
char_binary.delete_pchar(pc);
|
||||
5
Examples/test-suite/javascript/class_ignore_runme.js
Normal file
5
Examples/test-suite/javascript/class_ignore_runme.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
a = new class_ignore.Bar();
|
||||
|
||||
if (class_ignore.do_blah(a) != "Bar::blah")
|
||||
throw "Error";
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
f = new class_scope_weird.Foo();
|
||||
g = new class_scope_weird.Foo(3);
|
||||
if (f.bar(3) != 3)
|
||||
throw RuntimeError;
|
||||
21
Examples/test-suite/javascript/complextest_runme.js
Normal file
21
Examples/test-suite/javascript/complextest_runme.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
a = [-1,2];
|
||||
|
||||
expected = [-1, -2];
|
||||
|
||||
a_c = complextest.Conj(a);
|
||||
if (a_c.toString() != expected.toString())
|
||||
throw "Error in Conj(a)";
|
||||
|
||||
a_c_f = complextest.Conjf(a);
|
||||
if (a_c_f.toString() != expected.toString())
|
||||
throw "Error in Conjf(a)";
|
||||
|
||||
v = new complextest.VectorStdCplx();
|
||||
v.add([1,2]);
|
||||
v.add([2,3]);
|
||||
v.add([4,3]);
|
||||
v.add(1);
|
||||
|
||||
// TODO: how to check validity?
|
||||
complextest.Copy_h(v);
|
||||
31
Examples/test-suite/javascript/constover_runme.js
Normal file
31
Examples/test-suite/javascript/constover_runme.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
p = constover.test("test");
|
||||
if (p != "test") {
|
||||
throw "test failed!";
|
||||
}
|
||||
|
||||
p = constover.test_pconst("test");
|
||||
if (p != "test_pconst") {
|
||||
throw "test_pconst failed!";
|
||||
}
|
||||
|
||||
f = new constover.Foo();
|
||||
|
||||
p = f.test("test");
|
||||
if (p != "test") {
|
||||
throw "member-test failed!";
|
||||
}
|
||||
|
||||
p = f.test_pconst("test");
|
||||
if (p != "test_pconst") {
|
||||
throw "member-test_pconst failed!";
|
||||
}
|
||||
|
||||
p = f.test_constm("test");
|
||||
if (p != "test_constmethod") {
|
||||
throw "member-test_constm failed!";
|
||||
}
|
||||
|
||||
p = f.test_pconstm("test");
|
||||
if (p != "test_pconstmethod") {
|
||||
throw "member-test_pconstm failed!";
|
||||
}
|
||||
26
Examples/test-suite/javascript/cpp_enum_runme.js
Normal file
26
Examples/test-suite/javascript/cpp_enum_runme.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
var f = new cpp_enum.Foo()
|
||||
|
||||
if(f.hola != cpp_enum.Hello){
|
||||
print(f.hola);
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
f.hola = cpp_enum.Foo.Hi
|
||||
if(f.hola != cpp_enum.Foo.Hi){
|
||||
print(f.hola);
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
f.hola = cpp_enum.Hello
|
||||
|
||||
if(f.hola != cpp_enum.Hello){
|
||||
print(f.hola);
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
cpp_enum.Foo.hi = cpp_enum.Hello
|
||||
if(cpp_enum.Foo.hi != cpp_enum.Hello){
|
||||
print(cpp_enum.Foo.hi);
|
||||
throw "Error";
|
||||
}
|
||||
|
||||
45
Examples/test-suite/javascript/cpp_namespace_runme.js
Normal file
45
Examples/test-suite/javascript/cpp_namespace_runme.js
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
var n = cpp_namespace.fact(4);
|
||||
if (n != 24){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
if (cpp_namespace.Foo != 42){
|
||||
throw ("Bad variable value error!");
|
||||
}
|
||||
|
||||
t = new cpp_namespace.Test();
|
||||
if (t.method() != "Test::method"){
|
||||
throw ("Bad method return value error!");
|
||||
}
|
||||
if (cpp_namespace.do_method(t) != "Test::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
|
||||
if (cpp_namespace.do_method2(t) != "Test::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
cpp_namespace.weird("hello", 4);
|
||||
delete t;
|
||||
|
||||
t2 = new cpp_namespace.Test2();
|
||||
t3 = new cpp_namespace.Test3();
|
||||
t4 = new cpp_namespace.Test4();
|
||||
t5 = new cpp_namespace.Test5();
|
||||
if (cpp_namespace.foo3(42) != 42){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
|
||||
if (cpp_namespace.do_method3(t2,40) != "Test2::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
|
||||
if (cpp_namespace.do_method3(t3,40) != "Test3::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
|
||||
if (cpp_namespace.do_method3(t4,40) != "Test4::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
|
||||
if (cpp_namespace.do_method3(t5,40) != "Test5::method"){
|
||||
throw ("Bad return value error!");
|
||||
}
|
||||
7
Examples/test-suite/javascript/cpp_static_runme.js
Normal file
7
Examples/test-suite/javascript/cpp_static_runme.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
cpp_static.StaticFunctionTest.static_func();
|
||||
cpp_static.StaticFunctionTest.static_func_2(1);
|
||||
cpp_static.StaticFunctionTest.static_func_3(1,2);
|
||||
cpp_static.StaticMemberTest.static_int = 10;
|
||||
if (cpp_static.StaticMemberTest.static_int != 10)
|
||||
throw "error";
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
id = director_alternating.getBar().id();
|
||||
if (id != director_alternating.idFromGetBar())
|
||||
throw ("Error, Got wrong id: " + str(id));
|
||||
6
Examples/test-suite/javascript/enum_template_runme.js
Normal file
6
Examples/test-suite/javascript/enum_template_runme.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
if (enum_template.MakeETest() != 1)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (enum_template.TakeETest(0) != null)
|
||||
throw "RuntimeError";
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
x = new namespace_virtual_method.Spam();
|
||||
3
Examples/test-suite/javascript/overload_copy_runme.js
Normal file
3
Examples/test-suite/javascript/overload_copy_runme.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
f = new overload_copy.Foo();
|
||||
g = new overload_copy.Foo(f);
|
||||
22
Examples/test-suite/javascript/preproc_include_runme.js
Normal file
22
Examples/test-suite/javascript/preproc_include_runme.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
if (preproc_include.multiply10(10) != 100)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply20(10) != 200)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply30(10) != 300)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply40(10) != 400)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply50(10) != 500)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply60(10) != 600)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc_include.multiply70(10) != 700)
|
||||
throw "RuntimeError";
|
||||
|
||||
13
Examples/test-suite/javascript/preproc_runme.js
Normal file
13
Examples/test-suite/javascript/preproc_runme.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
if (preproc.endif != 1)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc.define != 1)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (preproc.defined != 1)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (2*preproc.one != preproc.two)
|
||||
throw "RuntimeError";
|
||||
|
||||
7
Examples/test-suite/javascript/ret_by_value_runme.js
Normal file
7
Examples/test-suite/javascript/ret_by_value_runme.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
a = ret_by_value.get_test();
|
||||
if (a.myInt != 100)
|
||||
throw "RuntimeError";
|
||||
|
||||
if (a.myShort != 200)
|
||||
throw "RuntimeError";
|
||||
10
Examples/test-suite/javascript/struct_value_runme.js
Normal file
10
Examples/test-suite/javascript/struct_value_runme.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
b = new struct_value.Bar();
|
||||
|
||||
b.a.x = 3;
|
||||
if (b.a.x != 3)
|
||||
throw "RuntimeError";
|
||||
|
||||
b.b.x = 3;
|
||||
if (b.b.x != 3)
|
||||
throw "RuntimeError"
|
||||
2
Examples/test-suite/javascript/template_static_runme.js
Normal file
2
Examples/test-suite/javascript/template_static_runme.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
template_static.Foo.bar_double(1);
|
||||
5
Examples/test-suite/javascript/typedef_class_runme.js
Normal file
5
Examples/test-suite/javascript/typedef_class_runme.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
a = new typedef_class.RealA();
|
||||
a.a = 3;
|
||||
|
||||
b = new typedef_class.B();
|
||||
b.testA(a);
|
||||
22
Examples/test-suite/javascript/typedef_inherit_runme.js
Normal file
22
Examples/test-suite/javascript/typedef_inherit_runme.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
a = new typedef_inherit.Foo();
|
||||
b = new typedef_inherit.Bar();
|
||||
|
||||
x = typedef_inherit.do_blah(a);
|
||||
if (x != "Foo::blah")
|
||||
print("Whoa! Bad return" + x);
|
||||
|
||||
x = typedef_inherit.do_blah(b);
|
||||
if (x != "Bar::blah")
|
||||
print("Whoa! Bad return" + x);
|
||||
|
||||
c = new typedef_inherit.Spam();
|
||||
d = new typedef_inherit.Grok();
|
||||
|
||||
x = typedef_inherit.do_blah2(c);
|
||||
if (x != "Spam::blah")
|
||||
print("Whoa! Bad return" + x);
|
||||
|
||||
x = typedef_inherit.do_blah2(d);
|
||||
if (x != "Grok::blah")
|
||||
print ("Whoa! Bad return" + x);
|
||||
11
Examples/test-suite/javascript/typedef_scope_runme.js
Normal file
11
Examples/test-suite/javascript/typedef_scope_runme.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
b = new typedef_scope.Bar();
|
||||
x = b.test1(42,"hello");
|
||||
if (x != 42)
|
||||
print("Failed!!");
|
||||
|
||||
x = b.test2(42,"hello");
|
||||
if (x != "hello")
|
||||
print("Failed!!");
|
||||
|
||||
|
||||
4
Examples/test-suite/javascript/typemap_arrays_runme.js
Normal file
4
Examples/test-suite/javascript/typemap_arrays_runme.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
if (typemap_arrays.sumA(null) != 60)
|
||||
throw "RuntimeError, Sum is wrong";
|
||||
|
||||
4
Examples/test-suite/javascript/typemap_delete_runme.js
Normal file
4
Examples/test-suite/javascript/typemap_delete_runme.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
r = new typemap_delete.Rect(123);
|
||||
if (r.val != 123)
|
||||
throw "RuntimeError";
|
||||
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