Pyrogenesis  trunk
ScriptInterface.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SCRIPTINTERFACE
19 #define INCLUDED_SCRIPTINTERFACE
20 
21 #include <boost/random/linear_congruential.hpp>
22 
23 #include "lib/file/vfs/vfs_path.h"
24 
25 #include "maths/Fixed.h"
26 #include "ScriptTypes.h"
27 #include "ps/Errors.h"
28 
29 ERROR_GROUP(Scripting);
30 ERROR_TYPE(Scripting, SetupFailed);
31 
32 ERROR_SUBGROUP(Scripting, LoadFile);
33 ERROR_TYPE(Scripting_LoadFile, OpenFailed);
34 ERROR_TYPE(Scripting_LoadFile, EvalErrors);
35 
36 ERROR_TYPE(Scripting, ConversionFailed);
37 ERROR_TYPE(Scripting, CallFunctionFailed);
38 ERROR_TYPE(Scripting, RegisterFunctionFailed);
39 ERROR_TYPE(Scripting, DefineConstantFailed);
40 ERROR_TYPE(Scripting, CreateObjectFailed);
41 ERROR_TYPE(Scripting, TypeDoesNotExist);
42 
43 ERROR_SUBGROUP(Scripting, DefineType);
44 ERROR_TYPE(Scripting_DefineType, AlreadyExists);
45 ERROR_TYPE(Scripting_DefineType, CreationFailed);
46 
47 // Set the maximum number of function arguments that can be handled
48 // (This should be as small as possible (for compiler efficiency),
49 // but as large as necessary for all wrapped functions)
50 #define SCRIPT_INTERFACE_MAX_ARGS 8
51 
52 // TODO: what's a good default?
53 #define DEFAULT_RUNTIME_SIZE 16 * 1024 * 1024
54 #define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024
55 
57 
59 
60 extern shared_ptr<ScriptRuntime> g_ScriptRuntime;
61 
62 
63 /**
64  * Abstraction around a SpiderMonkey JSContext.
65  *
66  * Thread-safety:
67  * - May be used in non-main threads.
68  * - Each ScriptInterface must be created, used, and destroyed, all in a single thread
69  * (it must never be shared between threads).
70  */
72 {
74 
75 public:
76 
77  /**
78  * Returns a runtime, which can used to initialise any number of
79  * ScriptInterfaces contexts. Values created in one context may be used
80  * in any other context from the same runtime (but not any other runtime).
81  * Each runtime should only ever be used on a single thread.
82  * @param runtimeSize Maximum size in bytes of the new runtime
83  */
84  static shared_ptr<ScriptRuntime> CreateRuntime(shared_ptr<ScriptRuntime> parentRuntime = shared_ptr<ScriptRuntime>(), int runtimeSize = DEFAULT_RUNTIME_SIZE,
85  int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
86 
87 
88  /**
89  * Constructor.
90  * @param nativeScopeName Name of global object that functions (via RegisterFunction) will
91  * be placed into, as a scoping mechanism; typically "Engine"
92  * @param debugName Name of this interface for CScriptStats purposes.
93  * @param runtime ScriptRuntime to use when initializing this interface.
94  */
95  ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptRuntime>& runtime);
96 
98 
99  struct CxPrivate
100  {
101  ScriptInterface* pScriptInterface; // the ScriptInterface object the current context belongs to
102  void* pCBData; // meant to be used as the "this" object for callback functions
103  } m_CxPrivate;
104 
105  void SetCallbackData(void* pCBData);
106  static CxPrivate* GetScriptInterfaceAndCBData(JSContext* cx);
107 
108  JSContext* GetContext() const;
109  JSRuntime* GetJSRuntime() const;
110  shared_ptr<ScriptRuntime> GetRuntime() const;
111 
112  /**
113  * Load global scripts that most script contexts need,
114  * located in the /globalscripts directory. VFS must be initialized.
115  */
116  bool LoadGlobalScripts();
117 
119  JS::Value GetCachedValue(CACHED_VAL valueIdentifier) const;
120 
121  /**
122  * Replace the default JS random number geenrator with a seeded, network-sync'd one.
123  */
124  bool ReplaceNondeterministicRNG(boost::rand48& rng);
125 
126  /**
127  * Call a constructor function, equivalent to JS "new ctor(arg)".
128  * @param ctor An object that can be used as constructor
129  * @param argv Constructor arguments
130  * @param out The new object; On error an error message gets logged and out is Null (out.isNull() == true).
131  */
132  void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const;
133 
134  JSObject* CreateCustomObject(const std::string & typeName) const;
135  void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
136 
137  jsval GetGlobalObject();
138 
139  JSClass* GetGlobalClass();
140 
141  /**
142  * Set the named property on the global object.
143  * If @p replace is true, an existing property will be overwritten; otherwise attempts
144  * to set an already-defined value will fail.
145  */
146  template<typename T>
147  bool SetGlobal(const char* name, const T& value, bool replace = false);
148 
149  /**
150  * Set the named property on the given object.
151  * Optionally makes it {ReadOnly, DontDelete, DontEnum}.
152  */
153  template<typename T>
154  bool SetProperty(JS::HandleValue obj, const char* name, const T& value, bool constant = false, bool enumerate = true);
155 
156  /**
157  * Set the named property on the given object.
158  * Optionally makes it {ReadOnly, DontDelete, DontEnum}.
159  */
160  template<typename T>
161  bool SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool constant = false, bool enumerate = true);
162 
163  /**
164  * Set the integer-named property on the given object.
165  * Optionally makes it {ReadOnly, DontDelete, DontEnum}.
166  */
167  template<typename T>
168  bool SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerate = true);
169 
170  /**
171  * Get the named property on the given object.
172  */
173  template<typename T>
174  bool GetProperty(JS::HandleValue obj, const char* name, T& out) const;
175 
176  /**
177  * Get the named property of the given object.
178  */
179  bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const;
180  bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) const;
181 
182  /**
183  * Get the integer-named property on the given object.
184  */
185  template<typename T>
186  bool GetPropertyInt(JS::HandleValue obj, int name, T& out) const;
187 
188  /**
189  * Get the named property of the given object.
190  */
191  bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const;
192 
193  /**
194  * Check the named property has been defined on the given object.
195  */
196  bool HasProperty(JS::HandleValue obj, const char* name) const;
197 
198  bool EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector<std::string>& out) const;
199 
200  bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
201 
202  bool FreezeObject(JS::HandleValue objVal, bool deep) const;
203 
204  bool Eval(const char* code) const;
205 
206  template<typename CHAR> bool Eval(const CHAR* code, JS::MutableHandleValue out) const;
207  template<typename T, typename CHAR> bool Eval(const CHAR* code, T& out) const;
208 
209  /**
210  * Convert an object to a UTF-8 encoded string, either with JSON
211  * (if pretty == true and there is no JSON error) or with toSource().
212  *
213  * We have to use a mutable handle because JS_Stringify requires that for unknown reasons.
214  */
215  std::string ToString(JS::MutableHandleValue obj, bool pretty = false) const;
216 
217  /**
218  * Parse a UTF-8-encoded JSON string. Returns the unmodified value on error
219  * and prints an error message.
220  * @return true on success; false otherwise
221  */
222  bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const;
223 
224  /**
225  * Read a JSON file. Returns the unmodified value on error and prints an error message.
226  */
227  void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const;
228 
229  /**
230  * Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
231  */
232  std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true) const;
233 
234  /**
235  * Report the given error message through the JS error reporting mechanism,
236  * and throw a JS exception. (Callers can check IsPendingException, and must
237  * return false in that case to propagate the exception.)
238  */
239  void ReportError(const char* msg) const;
240 
241  /**
242  * Load and execute the given script in a new function scope.
243  * @param filename Name for debugging purposes (not used to load the file)
244  * @param code JS code to execute
245  * @return true on successful compilation and execution; false otherwise
246  */
247  bool LoadScript(const VfsPath& filename, const std::string& code) const;
248 
249  /**
250  * Load and execute the given script in the global scope.
251  * @param filename Name for debugging purposes (not used to load the file)
252  * @param code JS code to execute
253  * @return true on successful compilation and execution; false otherwise
254  */
255  bool LoadGlobalScript(const VfsPath& filename, const std::wstring& code) const;
256 
257  /**
258  * Load and execute the given script in the global scope.
259  * @return true on successful compilation and execution; false otherwise
260  */
261  bool LoadGlobalScriptFile(const VfsPath& path) const;
262 
263  /**
264  * Construct a new value (usable in this ScriptInterface's context) by cloning
265  * a value from a different context.
266  * Complex values (functions, XML, etc) won't be cloned correctly, but basic
267  * types and cyclic references should be fine.
268  */
269  JS::Value CloneValueFromOtherContext(ScriptInterface& otherContext, JS::HandleValue val);
270 
271  /**
272  * Convert a jsval to a C++ type. (This might trigger GC.)
273  */
274  template<typename T> static bool FromJSVal(JSContext* cx, const JS::HandleValue val, T& ret);
275 
276  /**
277  * Convert a C++ type to a jsval. (This might trigger GC. The return
278  * value must be rooted if you don't want it to be collected.)
279  * NOTE: We are passing the JS::Value by reference instead of returning it by value.
280  * The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio.
281  * Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921
282  */
283  template<typename T> static void ToJSVal(JSContext* cx, JS::MutableHandleValue ret, T const& val);
284 
285  /**
286  * MaybeGC tries to determine whether garbage collection in cx's runtime would free up enough memory to be worth the amount of time it would take.
287  * This calls JS_MaybeGC directly, which does not do incremental GC. Usually you should prefer MaybeIncrementalRuntimeGC.
288  */
289  void MaybeGC();
290 
291  /**
292  * Triggers a full non-incremental garbage collection immediately. That should only be required in special cases and normally
293  * you should try to use MaybeIncrementalRuntimeGC instead.
294  */
295  void ForceGC();
296 
297  /**
298  * MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and
299  * returns the generated number.
300  * Math_random (with underscore, not this function) is a global function, but different random number generators can be
301  * stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance.
302  */
303  bool MathRandom(double& nbr);
304 
305  /**
306  * Structured clones are a way to serialize 'simple' JS values into a buffer
307  * that can safely be passed between contexts and runtimes and threads.
308  * A StructuredClone can be stored and read multiple times if desired.
309  * We wrap them in shared_ptr so memory management is automatic and
310  * thread-safe.
311  */
313  {
315  public:
316  StructuredClone();
317  ~StructuredClone();
319  size_t m_Size;
320  };
321 
322  shared_ptr<StructuredClone> WriteStructuredClone(JS::HandleValue v);
323  void ReadStructuredClone(const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret);
324 
325  /**
326  * Converts |a| if needed and assigns it to |handle|.
327  * This is meant for use in other templates where we want to use the same code for JS::RootedValue&/JS::HandleValue and
328  * other types. Note that functions are meant to take JS::HandleValue instead of JS::RootedValue&, but this implicit
329  * conversion does not work for templates (exact type matches required for type deduction).
330  * A similar functionality could also be implemented as a ToJSVal specialization. The current approach was preferred
331  * because "conversions" from JS::HandleValue to JS::MutableHandleValue are unusual and should not happen "by accident".
332  */
333  template <typename T>
334  static void AssignOrToJSVal(JSContext* cx, JS::MutableHandleValue handle, const T& a);
335 
336  /**
337  * The same as AssignOrToJSVal, but also allows JS::Value for T.
338  * In most cases it's not safe to use the plain (unrooted) JS::Value type, but this can happen quite
339  * easily with template functions. The idea is that the linker prints an error if AssignOrToJSVal is
340  * used with JS::Value. If the specialization for JS::Value should be allowed, you can use this
341  * "unrooted" version of AssignOrToJSVal.
342  */
343  template <typename T>
344  static void AssignOrToJSValUnrooted(JSContext* cx, JS::MutableHandleValue handle, const T& a)
345  {
346  AssignOrToJSVal(cx, handle, a);
347  }
348 
349  /**
350  * Converts |val| to T if needed or just returns it if it's a handle.
351  * This is meant for use in other templates where we want to use the same code for JS::HandleValue and
352  * other types.
353  */
354  template <typename T>
355  static T AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret);
356 
357 private:
358 
359  bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const;
360  bool Eval_(const char* code, JS::MutableHandleValue ret) const;
361  bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const;
362  bool SetGlobal_(const char* name, JS::HandleValue value, bool replace);
363  bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool readonly, bool enumerate);
364  bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool readonly, bool enumerate);
365  bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate);
366  bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const;
367  bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const;
368  static bool IsExceptionPending(JSContext* cx);
369  static const JSClass* GetClass(JS::HandleObject obj);
370  static void* GetPrivate(JS::HandleObject obj);
371 
372  struct CustomType
373  {
374  // TODO: Move assignment operator and move constructor only have to be
375  // explicitly defined for Visual Studio. VS2013 is still behind on C++11 support
376  // What's missing is what they call "Rvalue references v3.0", see
377  // https://msdn.microsoft.com/en-us/library/hh567368.aspx#rvref
380  {
381  m_Prototype = std::move(other.m_Prototype);
382  m_Class = std::move(other.m_Class);
383  m_Constructor = std::move(other.m_Constructor);
384  return *this;
385  }
387  {
388  m_Prototype = std::move(other.m_Prototype);
389  m_Class = std::move(other.m_Class);
390  m_Constructor = std::move(other.m_Constructor);
391  }
392 
393  JS::PersistentRootedObject m_Prototype;
394  JSClass* m_Class;
395  JSNative m_Constructor;
396  };
397  void Register(const char* name, JSNative fptr, size_t nargs);
398 
399  // Take care to keep this declaration before heap rooted members. Destructors of heap rooted
400  // members have to be called before the runtime destructor.
401  std::unique_ptr<ScriptInterface_impl> m;
402 
403  boost::rand48* m_rng;
404  std::map<std::string, CustomType> m_CustomObjectTypes;
405 
406 // The nasty macro/template bits are split into a separate file so you don't have to look at them
407 public:
408  #include "NativeWrapperDecls.h"
409  // This declares:
410  //
411  // template <R, T0..., TR (*fptr) (void* cbdata, T0...)>
412  // void RegisterFunction(const char* functionName);
413  //
414  // template <R, T0..., TR (*fptr) (void* cbdata, T0...)>
415  // static JSNative call;
416  //
417  // template <R, T0..., JSClass*, TC, TR (TC:*fptr) (T0...)>
418  // static JSNative callMethod;
419  //
420  // template <R, T0..., JSClass*, TC, TR (TC:*fptr) const (T0...)>
421  // static JSNative callMethodConst;
422  //
423  // template <T0...>
424  // static size_t nargs();
425  //
426  // template <R, T0...>
427  // bool CallFunction(JS::HandleValue val, const char* name, R& ret, const T0&...) const;
428  //
429  // template <R, T0...>
430  // bool CallFunction(JS::HandleValue val, const char* name, JS::Rooted<R>* ret, const T0&...) const;
431  //
432  // template <R, T0...>
433  // bool CallFunction(JS::HandleValue val, const char* name, JS::MutableHandle<R> ret, const T0&...) const;
434  //
435  // template <T0...>
436  // bool CallFunctionVoid(JS::HandleValue val, const char* name, const T0&...) const;
437 };
438 
439 // Implement those declared functions
440 #include "NativeWrapperDefns.h"
441 
442 template<typename T>
443 inline void ScriptInterface::AssignOrToJSVal(JSContext* cx, JS::MutableHandleValue handle, const T& a)
444 {
445  ToJSVal(cx, handle, a);
446 }
447 
448 template<>
449 inline void ScriptInterface::AssignOrToJSVal<JS::PersistentRootedValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::PersistentRootedValue& a)
450 {
451  handle.set(a);
452 }
453 
454 template<>
455 inline void ScriptInterface::AssignOrToJSVal<JS::RootedValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::RootedValue& a)
456 {
457  handle.set(a);
458 }
459 
460 template <>
461 inline void ScriptInterface::AssignOrToJSVal<JS::HandleValue>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::HandleValue& a)
462 {
463  handle.set(a);
464 }
465 
466 template <>
467 inline void ScriptInterface::AssignOrToJSValUnrooted<JS::Value>(JSContext* UNUSED(cx), JS::MutableHandleValue handle, const JS::Value& a)
468 {
469  handle.set(a);
470 }
471 
472 template<typename T>
473 inline T ScriptInterface::AssignOrFromJSVal(JSContext* cx, const JS::HandleValue& val, bool& ret)
474 {
475  T retVal;
476  ret = FromJSVal(cx, val, retVal);
477  return retVal;
478 }
479 
480 template<>
481 inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSContext* UNUSED(cx), const JS::HandleValue& val, bool& ret)
482 {
483  ret = true;
484  return val;
485 }
486 
487 template<typename T>
488 bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace)
489 {
490  JSAutoRequest rq(GetContext());
491  JS::RootedValue val(GetContext());
492  AssignOrToJSVal(GetContext(), &val, value);
493  return SetGlobal_(name, val, replace);
494 }
495 
496 template<typename T>
497 bool ScriptInterface::SetProperty(JS::HandleValue obj, const char* name, const T& value, bool readonly, bool enumerate)
498 {
499  JSAutoRequest rq(GetContext());
500  JS::RootedValue val(GetContext());
501  AssignOrToJSVal(GetContext(), &val, value);
502  return SetProperty_(obj, name, val, readonly, enumerate);
503 }
504 
505 template<typename T>
506 bool ScriptInterface::SetProperty(JS::HandleValue obj, const wchar_t* name, const T& value, bool readonly, bool enumerate)
507 {
508  JSAutoRequest rq(GetContext());
509  JS::RootedValue val(GetContext());
510  AssignOrToJSVal(GetContext(), &val, value);
511  return SetProperty_(obj, name, val, readonly, enumerate);
512 }
513 
514 template<typename T>
515 bool ScriptInterface::SetPropertyInt(JS::HandleValue obj, int name, const T& value, bool readonly, bool enumerate)
516 {
517  JSAutoRequest rq(GetContext());
518  JS::RootedValue val(GetContext());
519  AssignOrToJSVal(GetContext(), &val, value);
520  return SetPropertyInt_(obj, name, val, readonly, enumerate);
521 }
522 
523 template<typename T>
524 bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) const
525 {
526  JSContext* cx = GetContext();
527  JSAutoRequest rq(cx);
528  JS::RootedValue val(cx);
529  if (!GetProperty_(obj, name, &val))
530  return false;
531  return FromJSVal(cx, val, out);
532 }
533 
534 template<typename T>
535 bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) const
536 {
537  JSAutoRequest rq(GetContext());
538  JS::RootedValue val(GetContext());
539  if (!GetPropertyInt_(obj, name, &val))
540  return false;
541  return FromJSVal(GetContext(), val, out);
542 }
543 
544 template<typename CHAR>
545 bool ScriptInterface::Eval(const CHAR* code, JS::MutableHandleValue ret) const
546 {
547  if (!Eval_(code, ret))
548  return false;
549  return true;
550 }
551 
552 template<typename T, typename CHAR>
553 bool ScriptInterface::Eval(const CHAR* code, T& ret) const
554 {
555  JSAutoRequest rq(GetContext());
556  JS::RootedValue rval(GetContext());
557  if (!Eval_(code, &rval))
558  return false;
559  return FromJSVal(GetContext(), rval, ret);
560 }
561 
562 #endif // INCLUDED_SCRIPTINTERFACE
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const
Call a constructor function, equivalent to JS "new ctor(arg)".
Definition: ScriptInterface.cpp:512
void ReadJSONFile(const VfsPath &path, JS::MutableHandleValue out) const
Read a JSON file.
Definition: ScriptInterface.cpp:970
bool ReplaceNondeterministicRNG(boost::rand48 &rng)
Replace the default JS random number geenrator with a seeded, network-sync&#39;d one. ...
Definition: ScriptInterface.cpp:471
JS::Value GetCachedValue(CACHED_VAL valueIdentifier) const
Definition: ScriptInterface.cpp:437
bool FreezeObject(JS::HandleValue objVal, bool deep) const
Definition: ScriptInterface.cpp:814
Definition: ScriptInterface.h:118
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
bool HasProperty(JS::HandleValue obj, const char *name) const
Check the named property has been defined on the given object.
Definition: ScriptInterface.cpp:723
Definition: ScriptInterface.h:118
void ForceGC()
Triggers a full non-incremental garbage collection immediately.
Definition: ScriptInterface.cpp:1100
jsval GetGlobalObject()
Definition: ScriptInterface.cpp:583
CACHED_VAL
Definition: ScriptInterface.h:118
static CStr prefix
Definition: DllLoader.cpp:46
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
NONCOPYABLE(ScriptInterface)
void Register(const char *name, JSNative fptr, size_t nargs)
Definition: ScriptInterface.cpp:492
std::unique_ptr< ScriptInterface_impl > m
Definition: ScriptInterface.h:401
shared_ptr< ScriptRuntime > g_ScriptRuntime
Definition: GameSetup.cpp:122
static void AssignOrToJSVal(JSContext *cx, JS::MutableHandleValue handle, const T &a)
Converts |a| if needed and assigns it to |handle|.
Definition: ScriptInterface.h:443
ERROR_SUBGROUP(Scripting, LoadFile)
bool LoadGlobalScripts()
Load global scripts that most script contexts need, located in the /globalscripts directory...
Definition: ScriptInterface.cpp:445
shared_ptr< ScriptRuntime > GetRuntime() const
Definition: ScriptInterface.cpp:507
static const JSClass * GetClass(JS::HandleObject obj)
Definition: ScriptInterface.cpp:1084
u64 * m_Data
Definition: ScriptInterface.h:318
ERROR_GROUP(Scripting)
struct ScriptInterface::CxPrivate m_CxPrivate
char CHAR
Definition: wgl.h:60
uint64_t u64
Definition: types.h:40
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate)
Definition: ScriptInterface.cpp:652
Structured clones are a way to serialize &#39;simple&#39; JS values into a buffer that can safely be passed b...
Definition: ScriptInterface.h:312
bool CallFunction_(JS::HandleValue val, const char *name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const
Definition: ScriptInterface.cpp:565
static bool IsExceptionPending(JSContext *cx)
Definition: ScriptInterface.cpp:1078
ScriptInterface(const char *nativeScopeName, const char *debugName, const shared_ptr< ScriptRuntime > &runtime)
Constructor.
Definition: ScriptInterface.cpp:403
static size_t nargs()
Definition: ScriptInterface.h:93
bool GetPropertyInt(JS::HandleValue obj, int name, T &out) const
Get the integer-named property on the given object.
Definition: ScriptInterface.h:535
bool SetPropertyInt(JS::HandleValue obj, int name, const T &value, bool constant=false, bool enumerate=true)
Set the integer-named property on the given object.
Definition: ScriptInterface.h:515
JS::Value CloneValueFromOtherContext(ScriptInterface &otherContext, JS::HandleValue val)
Construct a new value (usable in this ScriptInterface&#39;s context) by cloning a value from a different ...
Definition: ScriptInterface.cpp:1106
Config::Value_type Value
Definition: json_spirit_value.h:181
~ScriptInterface()
Definition: ScriptInterface.cpp:417
bool SetProperty(JS::HandleValue obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
Definition: ScriptInterface.h:497
Definition: path.h:77
CustomType()
Definition: ScriptInterface.h:378
boost::mt19937 rng
Random number generator (Boost Mersenne Twister)
Definition: Noise.cpp:34
static CxPrivate * GetScriptInterfaceAndCBData(JSContext *cx)
Definition: ScriptInterface.cpp:431
void * pCBData
Definition: ScriptInterface.h:102
bool ParseJSON(const std::string &string_utf8, JS::MutableHandleValue out) const
Parse a UTF-8-encoded JSON string.
Definition: ScriptInterface.cpp:938
#define DEFAULT_RUNTIME_SIZE
Definition: ScriptInterface.h:53
#define T(string_literal)
Definition: secure_crt.cpp:76
CustomType & operator=(CustomType &&other)
Definition: ScriptInterface.h:379
CustomType(CustomType &&other)
Definition: ScriptInterface.h:386
bool SetGlobal_(const char *name, JS::HandleValue value, bool replace)
Definition: ScriptInterface.cpp:594
JS::PersistentRootedObject m_Prototype
Definition: ScriptInterface.h:393
#define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER
Definition: ScriptInterface.h:54
void SetCallbackData(void *pCBData)
Definition: ScriptInterface.cpp:426
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto)
Definition: ScriptInterface.cpp:804
JSObject * CreateCustomObject(const std::string &typeName) const
Definition: ScriptInterface.cpp:554
boost::rand48 * m_rng
Definition: ScriptInterface.h:403
static shared_ptr< ScriptRuntime > CreateRuntime(shared_ptr< ScriptRuntime > parentRuntime=shared_ptr< ScriptRuntime >(), int runtimeSize=DEFAULT_RUNTIME_SIZE, int heapGrowthBytesGCTrigger=DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER)
Returns a runtime, which can used to initialise any number of ScriptInterfaces contexts.
Definition: ScriptInterface.cpp:852
bool SetProperty_(JS::HandleValue obj, const char *name, JS::HandleValue value, bool readonly, bool enumerate)
Definition: ScriptInterface.cpp:615
bool MathRandom(double &nbr)
MathRandom (this function) calls the random number generator assigned to this ScriptInterface instanc...
Definition: ScriptInterface.cpp:324
bool GetProperty(JS::HandleValue obj, const char *name, T &out) const
Get the named property on the given object.
Definition: ScriptInterface.h:524
void ReportError(const char *msg) const
Report the given error message through the JS error reporting mechanism, and throw a JS exception...
Definition: ScriptInterface.cpp:1064
shared_ptr< StructuredClone > WriteStructuredClone(JS::HandleValue v)
Definition: ScriptInterface.cpp:1127
Definition: ScriptInterface.h:372
bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const
Definition: ScriptInterface.cpp:710
bool EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char *prefix, std::vector< std::string > &out) const
Definition: ScriptInterface.cpp:737
bool LoadGlobalScriptFile(const VfsPath &path) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:874
std::map< std::string, CustomType > m_CustomObjectTypes
Definition: ScriptInterface.h:404
unsigned int uint
Definition: types.h:42
bool SetGlobal(const char *name, const T &value, bool replace=false)
Set the named property on the global object.
Definition: ScriptInterface.h:488
JSClass * GetGlobalClass()
Definition: ScriptInterface.cpp:589
Definition: ScriptInterface.h:99
static void AssignOrToJSValUnrooted(JSContext *cx, JS::MutableHandleValue handle, const T &a)
The same as AssignOrToJSVal, but also allows JS::Value for T.
Definition: ScriptInterface.h:344
static bool FromJSVal(JSContext *cx, const JS::HandleValue val, T &ret)
Convert a jsval to a C++ type.
static Handle handle(size_t idx, u64 tag)
Definition: h_mgr.cpp:121
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
bool Eval(const char *code) const
Definition: ScriptInterface.cpp:909
void ReadStructuredClone(const shared_ptr< StructuredClone > &ptr, JS::MutableHandleValue ret)
Definition: ScriptInterface.cpp:1144
bool Eval_(const char *code, JS::MutableHandleValue ret) const
Definition: ScriptInterface.cpp:916
JSClass * m_Class
Definition: ScriptInterface.h:394
Abstraction around a SpiderMonkey JSRuntime.
Definition: ScriptRuntime.h:38
std::string ToString(JS::MutableHandleValue obj, bool pretty=false) const
Convert an object to a UTF-8 encoded string, either with JSON (if pretty == true and there is no JSON...
Definition: ScriptInterface.cpp:1027
bool LoadScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in a new function scope.
Definition: ScriptInterface.cpp:828
static T AssignOrFromJSVal(JSContext *cx, const JS::HandleValue &val, bool &ret)
Converts |val| to T if needed or just returns it if it&#39;s a handle.
Definition: ScriptInterface.h:473
JSNative m_Constructor
Definition: ScriptInterface.h:395
static void * GetPrivate(JS::HandleObject obj)
Definition: ScriptInterface.cpp:1089
ScriptInterface * pScriptInterface
Definition: ScriptInterface.h:101
JSContext * GetContext() const
Definition: ScriptInterface.cpp:497
void MaybeGC()
MaybeGC tries to determine whether garbage collection in cx&#39;s runtime would free up enough memory to ...
Definition: ScriptInterface.cpp:1095
JSRuntime * GetJSRuntime() const
Definition: ScriptInterface.cpp:502
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
Definition: ScriptInterface.cpp:526
size_t m_Size
Definition: ScriptInterface.h:319
std::string StringifyJSON(JS::MutableHandleValue obj, bool indent=true) const
Stringify to a JSON string, UTF-8 encoded.
Definition: ScriptInterface.cpp:1011
Definition: ScriptInterface.cpp:55
ERROR_TYPE(Scripting, SetupFailed)
bool LoadGlobalScript(const VfsPath &filename, const std::wstring &code) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:857
bool GetProperty_(JS::HandleValue obj, const char *name, JS::MutableHandleValue out) const
Definition: ScriptInterface.cpp:698
static void ToJSVal(JSContext *cx, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a jsval.