Ocean
JSScript.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_INTERACTION_JS_SCRIPT_H
9 #define META_OCEAN_INTERACTION_JS_SCRIPT_H
10 
12 
13 #include <v8.h>
14 
15 namespace Ocean
16 {
17 
18 namespace Interaction
19 {
20 
21 namespace JavaScript
22 {
23 
24 /**
25  * This class implements an object holding a JavaScript code.
26  * @ingroup interactionjs
27  */
28 class OCEAN_INTERACTION_JS_EXPORT JSScript
29 {
30  public:
31 
32  /**
33  * Creates a new script object.
34  */
35  JSScript() = default;
36 
37  /**
38  * Compiles and runs a script code.
39  * @param code Script code to compile
40  * @param errorMessage Resulting error message, if any
41  * @return True, if no error occurred during compilation
42  */
43  bool compileAndRun(const std::string& code, std::string& errorMessage);
44 
45  /**
46  * Runs the entire script code.
47  * @param returnValue Resulting return value, if any
48  * @param errorMessage Resulting error message, if any
49  * @return True, if no error occurred
50  */
51  bool run(v8::Handle<v8::Value>& returnValue, std::string& errorMessage);
52 
53  /**
54  * Returns whether the script has been compiled successfully.
55  * @return True, if so
56  */
57  inline bool isCompiled() const;
58 
59  /**
60  * Extracts the error message from a tryCatch object.
61  * @param tryCatch The tryCatch object from which the error will be extracted
62  * @return The extracted error message
63  */
64  static std::string extractErrorMessage(const v8::TryCatch& tryCatch);
65 
66  protected:
67 
68  /// Script handle object.
69  v8::UniquePersistent<v8::Script> script_;
70 };
71 
72 inline bool JSScript::isCompiled() const
73 {
74  return !script_.IsEmpty();
75 }
76 
77 }
78 
79 }
80 
81 }
82 
83 #endif // META_OCEAN_INTERACTION_JS_SCRIPT_H
This class implements an object holding a JavaScript code.
Definition: JSScript.h:29
bool run(v8::Handle< v8::Value > &returnValue, std::string &errorMessage)
Runs the entire script code.
bool isCompiled() const
Returns whether the script has been compiled successfully.
Definition: JSScript.h:72
bool compileAndRun(const std::string &code, std::string &errorMessage)
Compiles and runs a script code.
JSScript()=default
Creates a new script object.
static std::string extractErrorMessage(const v8::TryCatch &tryCatch)
Extracts the error message from a tryCatch object.
v8::UniquePersistent< v8::Script > script_
Script handle object.
Definition: JSScript.h:69
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15