{"id":2699,"date":"2020-05-04T09:32:49","date_gmt":"2020-05-04T06:32:49","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2699"},"modified":"2024-12-16T22:32:28","modified_gmt":"2024-12-16T19:32:28","slug":"add-javascript-support-for-cpp","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2020\/05\/04\/add-javascript-support-for-cpp\/","title":{"rendered":"Adding JavaScript Scripting Support to C++"},"content":{"rendered":"<p>In this note I will describe a way to add support for JavaScript scripts to a C++ application using the Tiny-JS library.<\/p>\n<p><a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D1%82%D0%B0%D1%80%D0%BE%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B0%D1%8F_%D0%B1%D0%B5%D1%80%D0%B5%D1%81%D1%82%D1%8F%D0%BD%D0%B0%D1%8F_%D0%B3%D1%80%D0%B0%D0%BC%D0%BE%D1%82%D0%B0_%E2%84%96_35\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2719\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/05\/lezha.png\" alt=\"\" width=\"320\" height=\"75\" srcset=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/05\/lezha.png 320w, https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2020\/05\/lezha-300x70.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p>Tiny-JS is a library for embedding in C++, providing execution of JavaScript code, with support for bindings (the ability to call C++ code from scripts)<\/p>\n<p>At first I wanted to use popular libraries ChaiScript, Duktape or connect Lua, but due to dependencies and possible difficulties in portability to different platforms, it was decided to find a simple, minimal, but powerful MIT JS lib, Tiny-JS meets these criteria. The only downside of this library is the lack of support\/development by the author, but its code is simple enough that you can take on the support yourself if necessary.<\/p>\n<p>Download Tiny-JS from the repository:<br \/><a href=\"https:\/\/github.com\/gfwilliams\/tiny-js\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/gfwilliams\/tiny-js<\/a><\/p>\n<p>Next, add Tiny-JS headers to the code that is responsible for scripts:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>#include \"tiny-js\/TinyJS.h\"\n#include \"tiny-js\/TinyJS_Functions.h\"\n\n<\/code><\/pre>\n<\/div>\n<p>Add TinyJS .cpp files to the build stage, then you can start writing scripts to load and run.<\/p>\n<p>An example of using the library is available in the repository:<br \/><a href=\"https:\/\/github.com\/gfwilliams\/tiny-js\/blob\/master\/Script.cpp\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/gfwilliams\/tiny-js\/blob\/master\/Script.cpp<\/a><br \/>\n<a href=\"https:\/\/github.com\/gfwilliams\/tiny-js\/blob\/wiki\/CodeExamples.md\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/gfwilliams\/tiny-js\/blob\/wiki\/CodeExamples.md<\/a><\/p>\n<p>An example of the implementation of the handler class can be found in the SpaceJaguar project:<br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/src\/Controllers\/SpaceJaguarScriptController\/SpaceJaguarScriptController.h\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/src\/Controllers\/SpaceJaguarScriptController\/SpaceJaguarScriptController.h<\/a><br \/>\n<a href=\"https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/src\/Controllers\/SpaceJaguarScriptController\/SpaceJaguarScriptController.cpp\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/src\/Controllers\/SpaceJaguarScriptController\/SpaceJaguarScriptController.cpp<\/a><\/p>\n<p>Example of a game script added to the application:<br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/resources\/com.demensdeum.spacejaguaractionrpg.scripts.sceneController.js\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/space-jaguar-action-rpg\/-\/blob\/master\/project\/resources\/com.demensdeum.spacejaguaractionrpg.scripts.sceneController.js<\/a><\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/github.com\/gfwilliams\/tiny-js\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/gfwilliams\/tiny-js<\/a ><br \/><a href=\"https:\/\/github.com\/dbohdan\/embedded-scripting-languages\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/dbohdan\/embedded-scripting-languages<\/a><br \/>\n<a href=\"https:\/\/github.com\/AlexKotik\/embeddable-scripting-languages\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/AlexKotik\/embeddable-scripting-languages<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this note I will describe a way to add support for JavaScript scripts to a C++ application using the Tiny-JS library. Tiny-JS is a library for embedding in C++, providing execution of JavaScript code, with support for bindings (the ability to call C++ code from scripts) At first I wanted to use popular libraries<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2020\/05\/04\/add-javascript-support-for-cpp\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Adding JavaScript Scripting Support to C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[],"class_list":["post-2699","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/comments?post=2699"}],"version-history":[{"count":26,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2699\/revisions"}],"predecessor-version":[{"id":3915,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2699\/revisions\/3915"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=2699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=2699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=2699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}