Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
8922bba
deps: update npm to 5.5.1
MylesBorins Oct 27, 2017
0ea8ff3
deps: backport 4ca695819 from npm upstream
MylesBorins Oct 27, 2017
237067d
deps: manually add 9.x support to npm
MylesBorins Oct 26, 2017
4fdd76d
http2: fix stream reading resumption
apapirovski Oct 29, 2017
ef238fb
v8: migrate setFlagsFromString to internal/errors
jasnell Oct 26, 2017
07d71c9
async_hooks: enable runtime checks by default
AndreasMadsen Oct 19, 2017
decb9e7
doc: add license information for remark-cli
MylesBorins Oct 31, 2017
ed92e0c
doc: add Gibson Fahnestock to Release team
gibfahn Oct 30, 2017
45873d2
module: revert #3384 DEP0019 EOL
MylesBorins Oct 31, 2017
daa286a
2017-10-31, Version 8.9.0 'Carbon' (LTS) Release
gibfahn Oct 31, 2017
fa9990f
2017-10-31, Version 9.0.0 (Current)
jasnell Sep 1, 2017
a933179
implement intialize(), tear_down(), process_events() for shared lib API
justus-hildebrand Dec 13, 2017
1906ddd
forgot to remove the node_lib include
justus-hildebrand Dec 18, 2017
f685a03
update ReadMe
justus-hildebrand Dec 18, 2017
8b01e1e
Update README.md
justus-hildebrand Dec 18, 2017
819c0f6
added nodelib interface v0.3
Dec 18, 2017
0280f31
Build on Travis (#6)
cmfcmf Dec 18, 2017
42a0104
revert changes made to node.cc and node.h
justus-hildebrand Jan 4, 2018
f4a5f64
Merge branch 'node_lib' of github.com:hpicgs/node into node_lib
justus-hildebrand Jan 4, 2018
d88e1d5
WIP: added more calling convenience
Jan 4, 2018
c9949a5
Renamed nodelib to node_lib
Jan 4, 2018
02493bb
Added Linux build commands to README
luminosuslight Jan 4, 2018
aa2c043
implement Terminate(), RequestTerminate(), RunEventLoop()
justus-hildebrand Jan 5, 2018
6ed7163
Short comments on what the Start() funcs do
justus-hildebrand Jan 5, 2018
2fae0db
Implemented Initialize(), Run() and Evaluate()
cmfcmf Jan 7, 2018
c01f4e5
Replace argc agrv in Initialize() with std::string
luminosuslight Jan 8, 2018
6acebd9
wip
msoechting Jan 8, 2018
4ad8cce
Cleaned code style (no indent for namespaces, left-lean asteriks, uni…
luminosuslight Jan 9, 2018
cc91ae3
Refactored cmd arg generation
luminosuslight Jan 9, 2018
4812646
Fixed cmd args
luminosuslight Jan 9, 2018
9a264ce
Moved code from Initialize() to new functions
luminosuslight Jan 9, 2018
ef6b4e5
Removed unused v8 argc argv
luminosuslight Jan 10, 2018
53f74b8
Implement remaining Call() methods
cmfcmf Jan 10, 2018
d5bb393
Re-add missing node.h include
cmfcmf Jan 14, 2018
d7d4b2b
Make RegisterModule work. Sadly, we can't use std::function|s, becaus…
cmfcmf Jan 14, 2018
f3e7a6d
fix issues from comments on PR #16
justus-hildebrand Jan 15, 2018
7a59273
Fix pointer to stack variable in register module
luminosuslight Jan 15, 2018
9175443
Use UV_RUN_NOWAIT, @justus-hildebrand
cmfcmf Jan 16, 2018
75f39b0
Merge branch 'interface-implementation' of github.com:hpicgs/node int…
cmfcmf Jan 16, 2018
674c0ee
add comments and TODOs for investigation
justus-hildebrand Jan 17, 2018
4e32b91
Implement `Deinitialize()` and `StopEventLoop()`
Jan 19, 2018
734d0d6
Don't read from stdin
cmfcmf Jan 19, 2018
0c575d5
Merge pull request #16 from hpicgs/interface-implementation
Jan 22, 2018
b945ac7
WIP usage
msoechting Jan 22, 2018
78329e0
Update usage documentation
msoechting Jan 22, 2018
6ea63e2
added code docu
Jan 22, 2018
a323279
refactored internals
Jan 24, 2018
6126411
Option to auto-call process.binding() when calling RegisterModule() (…
justus-hildebrand Jan 24, 2018
0ee4348
Rename getters to match style guide
cmfcmf Jan 29, 2018
200c1a9
added error handling via MaybeLocal (#40)
Jan 29, 2018
ee01950
Add support for cmd args (#39)
luminosuslight Jan 29, 2018
d7c8a33
Merge branch 'node_lib' into expose_internals
cmfcmf Jan 29, 2018
22a3107
Update README.md
EmberFlare Jan 29, 2018
5e7adc7
Merge branch 'node_lib' into expose_internals
EmberFlare Jan 29, 2018
e582066
early check for uv_loop_alive(), small refactoring
justus-hildebrand Jan 29, 2018
25211e6
Merge pull request #41 from hpicgs/expose_internals
Jan 30, 2018
6854d8f
changed the way internals are exposed.
Jan 30, 2018
807709d
included feedback from PR
Jan 30, 2018
c51f3f6
Fix method names
luminosuslight Jan 30, 2018
c5c73ff
fix nits
justus-hildebrand Jan 30, 2018
2bb42fb
Merge branch 'node_lib' into tick-event-loop-early-exit
justus-hildebrand Jan 30, 2018
ecee92a
Merge pull request #46 from hpicgs/tick-event-loop-early-exit
luminosuslight Jan 30, 2018
588ab28
moved 'EventLoopIsRunning' back to node::lib
Jan 30, 2018
b607c9f
Merge branch 'node_lib' into expose_internals
justus-hildebrand Jan 30, 2018
5539564
Merge pull request #47 from hpicgs/expose_internals
Jan 31, 2018
40baf2c
Merge branch 'node_lib' into lib-usage-documentation
EmberFlare Jan 31, 2018
e693ecf
added code docu
Jan 22, 2018
0ed0c77
included feedback from PR
Jan 30, 2018
2e6b1ff
fixed merge problems, added more docu for new methods
Jan 31, 2018
c913d75
Merge remote-tracking branch 'origin/interface_docu' into interface_docu
Jan 31, 2018
9c6bf17
Merge pull request #37 from hpicgs/interface_docu
EmberFlare Jan 31, 2018
cac4d43
Merge branch 'node_lib' into lib-usage-documentation
EmberFlare Jan 31, 2018
fd520a7
Merge pull request #35 from hpicgs/lib-usage-documentation
EmberFlare Jan 31, 2018
fe6a38a
Start fixing code style
luminosuslight Feb 1, 2018
334c3a6
Fix more code style, left leaning asterisks
luminosuslight Feb 1, 2018
f45be81
Fix C style cast
luminosuslight Feb 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added code docu
  • Loading branch information
Johannes Schneider committed Jan 22, 2018
commit 6ea63e2106e10b958d8c2e2ac835d20d24f84e82
230 changes: 161 additions & 69 deletions src/node_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,93 +9,148 @@

namespace node { namespace lib {

namespace { // private variables
bool _event_loop_running = false;
}

bool EventLoopIsRunning() { return _event_loop_running; }

/*********************************************************
* Function types
*********************************************************/

void _RegisterModuleCallback(v8::Local<v8::Object> exports,
namespace { // private definitions
bool _event_loop_running = false; /**< Holds the current state of the event loop. */

/**
* @brief Internal function to register a native C++ module.
*
* Internally used function to register a new C++ module for Node.js.
* @param exports The exports object, which is exposed to JavaScript.
* @param module The v8 module.
* @param context The current v8 context.
* @param priv Private data for the module.
*/
void _RegisterModuleCallback(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv);
}

/**
* @brief Indicates, whether the Node.js event loop is executed by `RunEventLoop`.
* @return True, if the Node.js event loop is executed by `RunEventLoop`. False otherwise.
*/
bool EventLoopIsRunning() { return _event_loop_running; }

/*********************************************************
* Start Node.js engine
*********************************************************/

/*
Starts the Node.js engine without a concrete script file to execute.
*Important*: This requires the C++ developer to call `ProcessEvents()` periodically OR call `RunMainLoop()` to start the uv event loop.
*/
/**
* @brief Starts the Node.js engine without executing a concrete script.
*
* Starts the Node.js engine by executing bootstrap code.
* This is required in order to load scripts (e.g. `Run`) or evaluate JavaScript code (e.g. `Evaluate`).
* Additionally, Node.js will not process any pending events caused by the JavaScript execution as long as
* `ProcessEvents` or `RunMainLoop` is not called.
* @param program_name The name for the Node.js application.
*/
NODE_EXTERN void Initialize(const std::string& program_name = "node_lib_executable");


/*
Stops the existing Node.js engine. Eventloop should not be running at this point.
*Important*: Once this was called, Initialize() will have to be called again for Node.js' library functions to be available again.
*/
/**
* @brief Stops the Node.js engine and destroys all current state.
*
* Stops the Node.js engine.
* This is done in two steps:
* 1. Issues the Node.js event loop to no longer accept any incoming events.
* 2. Waits for the event loop to be empty and then executes clean up code.
*/
NODE_EXTERN int Deinitialize();

/*
Executes a given JavaScript file and returns once the execution has finished.
*Important*: Node.js has to have been initialized by calling Initialize().
*/
/**
* @brief Executes the content of a given JavaScript file.
*
* Loads and executes the content of the given file.
* This method returns after the script was evaluated once.
* This means, that any pending events will not be processes as long as
* `ProcessEvents` or `RunEventLoop` is not called.
* @param path The path to the JavaScript file.
* @return The return value of the given JavaScript file.
*/
NODE_EXTERN v8::Local<v8::Value> Run(const std::string & path);

/*********************************************************
* Handle JavaScript events
*********************************************************/

/*
Processes the pending event queue of a *running* Node.js engine once.
*/
/**
* @brief Executes the Node.js event loop once.
*
* Processes all currently pending events in the Node.js event loop.
* This method returns immediately if there are no pending events.
* @return True, if more events need to be processed. False otherwise.
*/
NODE_EXTERN bool ProcessEvents();

/*
Starts the execution of the Node.js event loop, which processes any events in JavaScript.
Additionally, the given callback will be executed once per main loop run.
*Important*: Call `Initialize()` before using this method.
*/
/**
* @brief Starts the execution of the Node.js event loop. Calling the given callback once per loop tick.
*
* Executes the Node.js event loop as long as events keep coming.
* Once per loop execution, after events were processed, the given callback is executed.
* The event loop can be paused by calling `StopEventLoop`.
* @param callback The callback, which should be executed periodically while the calling thread is blocked.
*/
NODE_EXTERN void RunEventLoop(const std::function<void()> & callback);


/*********************************************************
* Stop Node.js engine
*********************************************************/

/*
Stops the Node.js event loop after its current execution. Execution can be resumed by calling RunEventLoop() again.
*/
/**
* @brief Issues the Node.js event loop to stop.
*
* Issues the Node.js event loop to stop.
* The event loop will finish its current execution.
* This means, that the loop is not stopped when this method returns.
* The execution can be resumed by using `RunEventLoop` again.
*/
NODE_EXTERN void StopEventLoop();

/*********************************************************
* Basic operations
*********************************************************/

/*
Executes a given piece of JavaScript code, using the *running* Node.js engine.
*/
/**
* @brief Evaluates the given JavaScript code.
*
* Parses and runs the given JavaScipt code.
* @param java_script_code The code to evaluate.
* @return The return value of the evaluated code.
*/
NODE_EXTERN v8::Local<v8::Value> Evaluate(const std::string & java_script_code);

/*
Returns the JavaScript root object for the running application
*/
/**
* @brief Returns the JavaScript root object.
*
* Returns the global root object for the current JavaScript context.
* @return The global root object.
*/
NODE_EXTERN v8::Local<v8::Object> GetRootObject();

/*
Registers a C++ module in the *running* Node.js engine.
*/
/**
* @brief Registers a native C++ module.
*
* Adds a native module to the Node.js engine.
* The module is initialized within the given callback. Additionally, private data can be included in the module
* using the priv pointer.
* The module can be used in JavaScript by calling `let cpp_module = process.binding('module_name')`.
* @param name The name for the module.
* @param callback The method, which initializes the module (e.g. by adding methods to the module).
* @param priv Any private data, which should be included within the module.
*/
NODE_EXTERN void RegisterModule(const std::string & name, const addon_context_register_func & callback, void *priv = nullptr);

/*
Registers a C++ module in the *running* Node.js engine exporting the given set of functions.
*/
/**
* @brief Registers a native C++ module.
*
* Adds a native module to the Node.js engine.
* Additionally, this method adds the given methods to the module.
* The module can be used in JavaScript by calling `let cpp_module = process.binding('module_name')`.
* @param name The name for the module.
* @param module_functions A list of functions and their names for the module.
*/
NODE_EXTERN void RegisterModule(const std::string & name,
const std::map<std::string, v8::FunctionCallback> & module_functions);

Expand All @@ -104,37 +159,74 @@ namespace node { namespace lib {
* Convenience operations
*********************************************************/

/*
Adds a new JavaScript module to the *running* Node.js engine.
*/
/**
* @brief Adds a NPM module to the current JavaScript context.
*
* Adds a given NPM module to the JavaScript context.
* This is achieved by calling `require('module_name')`.
* *Important* Make sure the NPM module is installed before using this method.
* @param module_name The name of the NPM module.
* @return The export object of the NPM module.
*/
NODE_EXTERN v8::Local<v8::Object> IncludeModule(const std::string & module_name);

/*
Returns the local value (specified by its name) of the module (defined in the `exports`-object).
*/
/**
* @brief Returns a member of the given object.
*
* Returns a member of the given object, specified by the members name.
* @param object The container for the requested value.
* @param value_name The name of the requested value.
* @return The requested value.
*/
NODE_EXTERN v8::MaybeLocal<v8::Value> GetValue(v8::MaybeLocal<v8::Object> object, const std::string & value_name);

/*
Calls a function (specified by its name) on a given object passing the given arguments.
*Important*: Throws an exception if the receiver does not define the specified function.
*/
/**
* @brief Calls a method on a given object.
*
* Calls a method on a given object.
* The function is retrieved by using the functions name.
* Additionally, a list of parameters is passed to the called function.
* @param object The container of the called function.
* @param function_name The name of the function to call.
* @param args The parameters to pass to the called function.
* @return The return value of the called function.
*/
NODE_EXTERN v8::Local<v8::Value> Call(v8::Local<v8::Object> object, const std::string & function_name, const std::vector<v8::Local<v8::Value>> & args = {});

/*
Calls a function (specified by its name) on a given object passing the given arguments.
*Important*: Throws an exception if the receiver does not define the specified function.
*/
/**
* @brief Calls a method on a given object.
*
* Calls a method on a given object.
* The function is retrieved by using the functions name.
* Additionally, a list of parameters is passed to the called function.
* @param object The container of the called function.
* @param function_name The name of the function to call.
* @param args The parameters to pass to the called function. The amount of arguments must be known at compile time.
* @return The return value of the called function.
*/
NODE_EXTERN v8::Local<v8::Value> Call(v8::Local<v8::Object> object, const std::string & function_name, std::initializer_list<v8::Local<v8::Value>> args);

/*
Calls a given function on a given receiver passing the given arguments.
*Important*: The amount of arguments can be changed at runtime (for JS var arg functions).
*/
/**
* @brief Calls a given method on a given object.
*
* Calls a given method on a given object.
* Additionally, a list of parameters is passed to the called function.
* @param object The receiver of the given function.
* @param function The function to be called.
* @param args The parameters to pass to the called function.
* @return The return value of the called function.
*/
NODE_EXTERN v8::Local<v8::Value> Call(v8::MaybeLocal<v8::Object> receiver, v8::MaybeLocal<v8::Function> function, const std::vector<v8::MaybeLocal<v8::Value>> & args = {});

/*
Calls a given function on a given receiver passing the given arguments.
*Important*: The amount of arguments must be known at compile time.
*/
/**
* @brief Calls a given method on a given object.
*
* Calls a given method on a given object.
* Additionally, a list of parameters is passed to the called function.
* @param object The receiver of the given function.
* @param function The function to be called.
* @param args The parameters to pass to the called function. The amount of arguments must be known at compile time.
* @return The return value of the called function.
*/
NODE_EXTERN v8::Local<v8::Value> Call(v8::MaybeLocal<v8::Object> receiver, v8::MaybeLocal<v8::Function> function, std::initializer_list<v8::MaybeLocal<v8::Value>> args);
}}