Coin  4.0.3
Coin3D core library
Loading...
Searching...
No Matches
SoJavaScriptEngine.h
1#ifndef COIN_SOJAVASCRIPTENGINE_H
2#define COIN_SOJAVASCRIPTENGINE_H
3
4/**************************************************************************\
5 * Copyright (c) Kongsberg Oil & Gas Technologies AS
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name of the copyright holder nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34\**************************************************************************/
35
36#include <Inventor/misc/SoScriptEngine.h>
37#include <Inventor/C/glue/spidermonkey.h>
38#include <Inventor/lists/SbList.h>
39
40// *************************************************************************
41
42#define COIN_JAVASCRIPT_RUNTIME_MAXBYTES 4194304
43
44// *************************************************************************
45
46typedef void SoJSfield2jsvalFunc(JSContext *, const SoField *, jsval *);
47typedef SbBool SoJSjsval2field2Func(JSContext *, const jsval, SoField *);
48typedef JSObject * SoJSWrapperInitFunc(JSContext *, JSObject * obj);
49
50// *************************************************************************
51
53
54public:
56 virtual ~SoJavaScriptEngine();
57
58 virtual SbBool executeScript(const SbName & name,
59 const SbString & script) const;
60 virtual SbBool executeFile(const SbName & filename) const;
61 virtual SbBool executeFunction(const SbName &name, int argc,
62 const SoField * argv,
63 SoField * rval = NULL) const;
64
65 virtual SbBool setScriptField(const SbName & name, const SoField * f) const;
66 virtual SbBool unsetScriptField(const SbName & name) const;
67 virtual SbBool getScriptField(const SbName & name, SoField * f) const;
68 virtual SbBool hasScriptField(const SbName & name) const;
69
70 // Everything under here is javascript specific
71
72 static SoJavaScriptEngine *getEngine(JSContext * cx);
73
74 SbBool field2jsval(const SoField * f, jsval * v) const;
75 SbBool jsval2field(const jsval v, SoField * f) const;
76 void enableAutoNodeUnref(SbBool onoff);
77 SbBool getAutoNodeUnrefState(void) const;
78
79 static SbBool init(uint32_t maxbytes = COIN_JAVASCRIPT_RUNTIME_MAXBYTES);
80 static void shutdown(void);
81 static SbBool debug(void);
82
83 static JSRuntime * getRuntime(void);
84 JSContext * getContext(void) const;
85 JSObject * getGlobal(void) const;
86
87 void addHandler(const SoType & type, SoJSWrapperInitFunc * init,
88 SoJSfield2jsvalFunc * field2jsval,
89 SoJSjsval2field2Func * jsval2field);
90
91protected:
92 static void setRuntime(JSRuntime * runtime);
93 void setContext(JSContext * context);
94 void setGlobal(JSObject * global);
95
96private:
97 class SoJavaScriptEngineP * pimpl;
98 friend class SoJavaScriptEngineP;
99};
100
101#endif // !COIN_SOJAVASCRIPTENGINE_H
The SbList class is a template container class for lists.
Definition SbList.h:70
The SbName class stores strings by reference.
Definition SbName.h:40
The SbString class is a string class with convenience functions for string operations.
Definition SbString.h:52
The SoField class is the top-level abstract base class for fields.
Definition SoField.h:47
The SoJavaScriptEngine class is yet to be documented.
Definition SoJavaScriptEngine.h:52
Definition SoScriptEngine.h:40
The SoType class is the basis for the runtime type system in Coin.
Definition SoType.h:59