diff --git a/.gitignore b/.gitignore index 514e756904..8cea976013 100644 --- a/.gitignore +++ b/.gitignore @@ -169,6 +169,9 @@ Release/ /src/mod/say/mod_say_nl/Makefile /src/mod/say/mod_say_th/Makefile /src/mod/say/mod_say_zh/Makefile +/src/mod/languages/mod_v8/Makefile +/src/mod/languages/mod_v8/Makefile.in +/src/mod/languages/mod_v8/mod_v8.log BuildLog.htm diff --git a/Freeswitch.2012.sln b/Freeswitch.2012.sln index 2d4ad4a9cc..6c823b105c 100644 --- a/Freeswitch.2012.sln +++ b/Freeswitch.2012.sln @@ -87,6 +87,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "autoload_configs", "autoloa conf\vanilla\autoload_configs\spidermonkey.conf.xml = conf\vanilla\autoload_configs\spidermonkey.conf.xml conf\vanilla\autoload_configs\switch.conf.xml = conf\vanilla\autoload_configs\switch.conf.xml conf\vanilla\autoload_configs\syslog.conf.xml = conf\vanilla\autoload_configs\syslog.conf.xml + conf\vanilla\autoload_configs\v8.conf.xml = conf\vanilla\autoload_configs\v8.conf.xml conf\vanilla\autoload_configs\voicemail.conf.xml = conf\vanilla\autoload_configs\voicemail.conf.xml conf\vanilla\autoload_configs\wanpipe.conf.xml = conf\vanilla\autoload_configs\wanpipe.conf.xml conf\vanilla\autoload_configs\woomera.conf.xml = conf\vanilla\autoload_configs\woomera.conf.xml @@ -195,6 +196,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libteletone", "libs\libtele EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_spidermonkey", "src\mod\languages\mod_spidermonkey\mod_spidermonkey.2012.vcxproj", "{1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_v8", "src\mod\languages\mod_v8\mod_v8.2012.vcxproj", "{9B9D2551-D6BD-4F20-8BE5-DE30E154A064}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_v8_skel", "src\mod\languages\mod_v8\mod_v8_skel.2012.vcxproj", "{8B754330-A434-4791-97E5-1EE67060BAC0}" +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_cepstral", "src\mod\asr_tts\mod_cepstral\mod_cepstral.2012.vcxproj", "{692F6330-4D87-4C82-81DF-40DB5892636E}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_ilbc", "src\mod\codecs\mod_ilbc\mod_ilbc.2012.vcxproj", "{D3EC0AFF-76FC-4210-A825-9A17410660A3}" @@ -577,6 +582,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcbt", "libs\win32\libcbt EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "make_cielab_luts", "libs\spandsp\src\msvc\make_cielab_luts.2012.vcxproj", "{85F0CF8C-C7AB-48F6-BA19-CC94CF87F981}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Download V8", "libs\win32\Download V8.2012.vcxproj", "{13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libv8", "libs\win32\v8\libv8.2012.vcxproj", "{AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}" +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Download OPUS", "libs\win32\Download OPUS.2012.vcxproj", "{092124C9-09ED-43C7-BD6D-4AE5D6B3C547}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "opus", "opus", "{ED2CA8B5-8E91-4296-A120-02BB0B674652}" @@ -839,6 +848,38 @@ Global {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F}.Release|x64.Build.0 = Release|x64 {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F}.Release|x64 Setup.ActiveCfg = Release|x64 {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F}.Release|x86 Setup.ActiveCfg = Release|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|Win32.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|x64.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|x64.Build.0 = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|x64 Setup.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|x64 Setup.Build.0 = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.All|x86 Setup.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|Win32.ActiveCfg = Debug|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|Win32.Build.0 = Debug|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|x64.ActiveCfg = Debug|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|x64.Build.0 = Debug|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|x64 Setup.ActiveCfg = Debug|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Debug|x86 Setup.ActiveCfg = Debug|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|Win32.ActiveCfg = Release|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|Win32.Build.0 = Release|Win32 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|x64.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|x64.Build.0 = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|x64 Setup.ActiveCfg = Release|x64 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064}.Release|x86 Setup.ActiveCfg = Release|Win32 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|Win32.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|x64.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|x64.Build.0 = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|x64 Setup.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|x64 Setup.Build.0 = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.All|x86 Setup.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Debug|Win32.ActiveCfg = Debug|Win32 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Debug|x64.ActiveCfg = Debug|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Debug|x64 Setup.ActiveCfg = Debug|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Debug|x86 Setup.ActiveCfg = Debug|Win32 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Release|Win32.ActiveCfg = Release|Win32 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Release|x64.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Release|x64 Setup.ActiveCfg = Release|x64 + {8B754330-A434-4791-97E5-1EE67060BAC0}.Release|x86 Setup.ActiveCfg = Release|Win32 {692F6330-4D87-4C82-81DF-40DB5892636E}.All|Win32.ActiveCfg = Release|x64 {692F6330-4D87-4C82-81DF-40DB5892636E}.All|x64.ActiveCfg = Release|x64 {692F6330-4D87-4C82-81DF-40DB5892636E}.All|x64.Build.0 = Release|x64 @@ -3838,6 +3879,41 @@ Global {85F0CF8C-C7AB-48F6-BA19-CC94CF87F981}.Release|x64.Build.0 = All|Win32 {85F0CF8C-C7AB-48F6-BA19-CC94CF87F981}.Release|x64 Setup.ActiveCfg = All|Win32 {85F0CF8C-C7AB-48F6-BA19-CC94CF87F981}.Release|x86 Setup.ActiveCfg = All|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.All|Win32.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.All|Win32.Build.0 = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.All|x64.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.All|x64 Setup.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.All|x86 Setup.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|Win32.ActiveCfg = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|Win32.Build.0 = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|x64.ActiveCfg = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|x64.Build.0 = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|x64 Setup.ActiveCfg = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Debug|x86 Setup.ActiveCfg = Debug|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|Win32.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|Win32.Build.0 = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|x64.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|x64.Build.0 = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|x64 Setup.ActiveCfg = Release|Win32 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF}.Release|x86 Setup.ActiveCfg = Release|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|Win32.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|x64.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|x64.Build.0 = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|x64 Setup.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|x64 Setup.Build.0 = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.All|x86 Setup.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|Win32.ActiveCfg = Debug|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|Win32.Build.0 = Debug|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|x64.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|x64.Build.0 = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|x64 Setup.ActiveCfg = Debug|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Debug|x86 Setup.ActiveCfg = Debug|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|Win32.ActiveCfg = Release|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|Win32.Build.0 = Release|Win32 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|x64.ActiveCfg = Release|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|x64.Build.0 = Release|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|x64 Setup.ActiveCfg = Release|x64 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2}.Release|x86 Setup.ActiveCfg = Release|Win32 {092124C9-09ED-43C7-BD6D-4AE5D6B3C547}.All|Win32.ActiveCfg = Release|Win32 {092124C9-09ED-43C7-BD6D-4AE5D6B3C547}.All|Win32.Build.0 = Release|Win32 {092124C9-09ED-43C7-BD6D-4AE5D6B3C547}.All|x64.ActiveCfg = Release|Win32 @@ -4098,10 +4174,13 @@ Global {2386B892-35F5-46CF-A0F0-10394D2FBF9B} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {77BC1DD2-C9A1-44D7-BFFA-1320370CACB9} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {85F0CF8C-C7AB-48F6-BA19-CC94CF87F981} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {ED2CA8B5-8E91-4296-A120-02BB0B674652} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {EDDB8AB9-C53E-44C0-A620-0E86C2CBD5D5} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {EC3E5C7F-EE09-47E2-80FE-546363D14A98} = {B8F5B47B-8568-46EB-B320-64C17D2A98BC} {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F} = {0C808854-54D1-4230-BFF5-77B5FD905000} + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064} = {0C808854-54D1-4230-BFF5-77B5FD905000} + {8B754330-A434-4791-97E5-1EE67060BAC0} = {0C808854-54D1-4230-BFF5-77B5FD905000} {ACFFF684-4D19-4D48-AF12-88EA1D778BDF} = {0C808854-54D1-4230-BFF5-77B5FD905000} {8F992C49-6C51-412F-B2A3-34EAB708EB65} = {0C808854-54D1-4230-BFF5-77B5FD905000} {0A6B5EA5-6E9B-4A51-931F-ED25AA87B4DF} = {0C808854-54D1-4230-BFF5-77B5FD905000} @@ -4139,6 +4218,7 @@ Global {B808178B-82F0-4CF4-A2B1-921939FA24D0} = {C120A020-773F-4EA3-923F-B67AF28B750D} {D578E676-7EC8-4548-BD8B-845C635F14AD} = {C120A020-773F-4EA3-923F-B67AF28B750D} {652AD5F7-8488-489F-AAD0-7FBE064703B6} = {C120A020-773F-4EA3-923F-B67AF28B750D} + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF} = {C120A020-773F-4EA3-923F-B67AF28B750D} {092124C9-09ED-43C7-BD6D-4AE5D6B3C547} = {C120A020-773F-4EA3-923F-B67AF28B750D} {988CACF7-3FCB-4992-BE69-77872AE67DC8} = {6CD61A1D-797C-470A-BE08-8C31B68BB336} {5BC072DB-3826-48EA-AF34-FE32AA01E83B} = {6CD61A1D-797C-470A-BE08-8C31B68BB336} diff --git a/build/modules.conf.in b/build/modules.conf.in index b4c6f70888..727ec9191a 100644 --- a/build/modules.conf.in +++ b/build/modules.conf.in @@ -119,6 +119,7 @@ languages/mod_lua #languages/mod_perl #languages/mod_python #languages/mod_spidermonkey +languages/mod_v8 #languages/mod_yaml loggers/mod_console loggers/mod_logfile diff --git a/conf/curl/autoload_configs/v8.conf.xml b/conf/curl/autoload_configs/v8.conf.xml new file mode 100644 index 0000000000..01c9e2bd99 --- /dev/null +++ b/conf/curl/autoload_configs/v8.conf.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/conf/insideout/autoload_configs/modules.conf.xml b/conf/insideout/autoload_configs/modules.conf.xml index 692a362977..b15595c466 100644 --- a/conf/insideout/autoload_configs/modules.conf.xml +++ b/conf/insideout/autoload_configs/modules.conf.xml @@ -79,7 +79,8 @@ - + + diff --git a/conf/insideout/autoload_configs/v8.conf.xml b/conf/insideout/autoload_configs/v8.conf.xml new file mode 100644 index 0000000000..01c9e2bd99 --- /dev/null +++ b/conf/insideout/autoload_configs/v8.conf.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/conf/vanilla/autoload_configs/modules.conf.xml b/conf/vanilla/autoload_configs/modules.conf.xml index 27fde61a64..eb4a8f4bd7 100644 --- a/conf/vanilla/autoload_configs/modules.conf.xml +++ b/conf/vanilla/autoload_configs/modules.conf.xml @@ -110,6 +110,7 @@ + diff --git a/conf/vanilla/autoload_configs/v8.conf.xml b/conf/vanilla/autoload_configs/v8.conf.xml new file mode 100644 index 0000000000..01c9e2bd99 --- /dev/null +++ b/conf/vanilla/autoload_configs/v8.conf.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/configure.in b/configure.in index 0b1cba9b94..f3b804aabf 100644 --- a/configure.in +++ b/configure.in @@ -1162,6 +1162,7 @@ AC_CONFIG_FILES([Makefile src/mod/legacy/languages/mod_lua/Makefile src/mod/languages/mod_python/Makefile src/mod/languages/mod_spidermonkey/Makefile + src/mod/languages/mod_v8/Makefile src/mod/event_handlers/mod_erlang_event/Makefile src/include/switch_am_config.h build/getsounds.sh diff --git a/debian/util.sh b/debian/util.sh index a767f3ba4f..4029985be0 100755 --- a/debian/util.sh +++ b/debian/util.sh @@ -115,6 +115,7 @@ getlibs () { getlib http://files.freeswitch.org/downloads/libs/lame-3.98.4.tar.gz getlib http://files.freeswitch.org/downloads/libs/libshout-2.2.2.tar.gz getlib http://files.freeswitch.org/downloads/libs/mpg123-1.13.2.tar.gz + getlib http://files.freeswitch.org/downloads/libs/v8-3.24.14.tar.bz2 # cleanup mongo ( cd mongo-cxx-driver-v1.8 diff --git a/docs/AUTHORS b/docs/AUTHORS index 6e51a00585..93964dc388 100644 --- a/docs/AUTHORS +++ b/docs/AUTHORS @@ -38,6 +38,7 @@ that much better: Juan Jose Comellas - Patch to switch_utils for arg parsing. Dale Thatcher - Additions to mod_conference. Simon Perreault & Marc Blanchet from Viagenie.ca - IPv6 Support. + Peter Olsson - mod_v8, and other various patches. A big THANK YOU goes to: diff --git a/freeswitch.spec b/freeswitch.spec index c53ee53d14..6c7e08ad2b 100644 --- a/freeswitch.spec +++ b/freeswitch.spec @@ -3,7 +3,7 @@ # spec file for package freeswitch # # includes module(s): freeswitch-devel freeswitch-codec-passthru-amr freeswitch-codec-passthru-amrwb freeswitch-codec-passthru-g729 -# freeswitch-codec-passthru-g7231 freeswitch-lua freeswitch-perl freeswitch-python freeswitch-spidermonkey +# freeswitch-codec-passthru-g7231 freeswitch-lua freeswitch-perl freeswitch-python freeswitch-spidermonkey freeswitch-v8 # freeswitch-lan-de freeswitch-lang-en freeswitch-lang-fr freeswitch-lang-hu freeswitch-lang-ru freeswitch-freetdm # # Initial Version Copyright (C) 2007 Peter Nixon and Michal Bielicki, All Rights Reserved. @@ -127,6 +127,7 @@ Source9: http://files.freeswitch.org/downloads/libs/communicator_semi_6000_20080 Source10: http://files.freeswitch.org/downloads/libs/libmemcached-0.32.tar.gz Source11: http://files.freeswitch.org/downloads/libs/json-c-0.9.tar.gz Source12: http://files.freeswitch.org/downloads/libs/opus-1.1.tar.gz +Source13: http://files.freeswitch.org/downloads/libs/v8-3.24.14.tar.bz2 Prefix: %{prefix} @@ -1102,10 +1103,17 @@ Requires: python %package spidermonkey Summary: JavaScript support for the FreeSWITCH open source telephony platform Group: System/Libraries -Requires: %{name} = %{version}-%{release} +Requires: %{name} = %{version}-%{release} %description spidermonkey +%package v8 +Summary: JavaScript support for the FreeSWITCH open source telephony platform, using Google V8 JavaScript engine +Group: System/Libraries +Requires: %{name} = %{version}-%{release} + +%description v8 + ###################################################################################################################### # FreeSWITCH Say Modules ###################################################################################################################### @@ -1421,7 +1429,7 @@ FORMATS_MODULES+=" formats/mod_ssml" # Embedded Languages # ###################################################################################################################### -LANGUAGES_MODULES="languages/mod_lua languages/mod_perl languages/mod_python languages/mod_spidermonkey" +LANGUAGES_MODULES="languages/mod_lua languages/mod_perl languages/mod_python languages/mod_spidermonkey languages/mod_v8" ###################################################################################################################### # @@ -2302,6 +2310,15 @@ fi %dir %attr(0750, freeswitch, daemon) %{sysconfdir}/autoload_configs %config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/spidermonkey.conf.xml +%files v8 +%defattr(-,freeswitch,daemon) +%{MODINSTDIR}/mod_v8*.so* +%{LIBDIR}/libv8.so +%{LIBDIR}/libicui18n.so +%{LIBDIR}/libicuuc.so +%dir %attr(0750, freeswitch, daemon) %{sysconfdir}/autoload_configs +%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/v8.conf.xml + ###################################################################################################################### # # Language Modules @@ -2427,6 +2444,8 @@ fi # ###################################################################################################################### %changelog +* Mon Jan 13 2014 - peter@olssononline.se +- Add mod_v8 * Mon Dec 09 2013 - crienzo@grasshopper.com - Add mod_ssml, mod_rayo - Fix build on master diff --git a/libs/.gitignore b/libs/.gitignore index 8fca0c8c42..4f048d2620 100644 --- a/libs/.gitignore +++ b/libs/.gitignore @@ -980,6 +980,7 @@ opal /zeromq-*/ /jpeg-8d/ +/v8-*/ # build products we should remove !/apr-util/xml/expat/conftools/config.guess diff --git a/libs/win32/Download V8.2012.vcxproj b/libs/win32/Download V8.2012.vcxproj new file mode 100644 index 0000000000..3760ca1298 --- /dev/null +++ b/libs/win32/Download V8.2012.vcxproj @@ -0,0 +1,82 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + Download V8 + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF} + Download V8 + Win32Proj + + + + Utility + MultiByte + v110 + + + Utility + MultiByte + v110 + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(PlatformName)\V8\$(Configuration)\ + $(PlatformName)\V8\$(Configuration)\ + + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + Document + Downloading V8. + if not exist "$(ProjectDir)..\v8-3.24.14" cscript /nologo "$(ProjectDir)util.vbs" GetUnzip http://files.freeswitch.org/downloads/libs/v8-3.24.14.tar.bz2 "$(ProjectDir).." +if not exist "$(ProjectDir)..\v8-3.24.14\third_party\cygwin" cscript /nologo "$(ProjectDir)util.vbs" GetUnzip http://files.freeswitch.org/downloads/libs/v8-3.24.14-win.tar.bz2 "$(ProjectDir).." + + $(ProjectDir)..\v8-3.24.14;%(Outputs) + Downloading V8. + if not exist "$(ProjectDir)..\v8-3.24.14" cscript /nologo "$(ProjectDir)util.vbs" GetUnzip http://files.freeswitch.org/downloads/libs/v8-3.24.14.tar.bz2 "$(ProjectDir).." +if not exist "$(ProjectDir)..\v8-3.24.14\third_party\cygwin" cscript /nologo "$(ProjectDir)util.vbs" GetUnzip http://files.freeswitch.org/downloads/libs/v8-3.24.14-win.tar.bz2 "$(ProjectDir).." + + $(ProjectDir)..\v8-3.24.14;%(Outputs) + + + + + + diff --git a/libs/win32/v8/build-v8.bat b/libs/win32/v8/build-v8.bat new file mode 100644 index 0000000000..f866d6deb1 --- /dev/null +++ b/libs/win32/v8/build-v8.bat @@ -0,0 +1,59 @@ +@ECHO OFF + +IF "%1" == "" GOTO Fail +IF "%2" == "" GOTO Fail +IF "%3" == "" GOTO Fail + +cd "%3" + +SET /P LAST_BUILD_INFO= last_build + +exit + +:Fail +exit /b 1 diff --git a/libs/win32/v8/libv8.2012.vcxproj b/libs/win32/v8/libv8.2012.vcxproj new file mode 100644 index 0000000000..827cd5d65e --- /dev/null +++ b/libs/win32/v8/libv8.2012.vcxproj @@ -0,0 +1,144 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + libv8 + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2} + libv8 + Win32Proj + + + + Utility + MultiByte + v110 + + + Utility + MultiByte + v110 + + + Utility + MultiByte + v110 + + + Utility + MultiByte + v110 + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(PlatformName)\V8\$(Configuration)\ + $(PlatformName)\V8\$(Configuration)\ + $(PlatformName)\V8\$(Configuration)\ + $(PlatformName)\V8\$(Configuration)\ + + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + X64 + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + X64 + + + $(IntDir)BuildLog $(ProjectName).htm + + + + + + + + + Document + Building Google V8 + $(ProjectDir)build-v8.bat x86 Release "$(ProjectDir)..\..\v8-3.24.14" + + $(ProjectDir)..\..\v8-3.24.14;%(Outputs) + Building Google V8 + $(ProjectDir)build-v8.bat x86 Debug "$(ProjectDir)..\..\v8-3.24.14" + + $(ProjectDir)..\..\v8-3.24.14;%(Outputs) + Building Google V8 + $(ProjectDir)build-v8.bat x64 Release "$(ProjectDir)..\..\v8-3.24.14" + + $(ProjectDir)..\..\v8-3.24.14;%(Outputs) + Building Google V8 + $(ProjectDir)build-v8.bat x64 Debug "$(ProjectDir)..\..\v8-3.24.14" + + $(ProjectDir)..\..\v8-3.24.14;%(Outputs) + + + + + {13077E43-E5DF-4EF6-9EB1-2A0B22E8DFAF} + false + + + + + + diff --git a/src/mod/languages/mod_v8/Makefile.am b/src/mod/languages/mod_v8/Makefile.am new file mode 100644 index 0000000000..1c54814728 --- /dev/null +++ b/src/mod/languages/mod_v8/Makefile.am @@ -0,0 +1,59 @@ +include $(top_srcdir)/build/modmake.rulesam + +V8=v8-3.24.14 + +V8_DIR=$(switch_srcdir)/libs/$(V8) +V8_BUILDDIR=$(switch_builddir)/libs/$(V8) +V8SO=$(V8_DIR)/out/native/lib.target/libv8.so + +CURL_DIR=$(switch_srcdir)/libs/curl +CURL_BUILDDIR=$(switch_builddir)/libs/curl +CURLLA=${switch_builddir}/libs/curl/lib/libcurl.la + +MODNAME=mod_v8 + +AM_CFLAGS += -I. -I./include -I$(V8_DIR)/include +AM_CPPFLAGS += -I. -I./include -I$(V8_DIR)/include +AM_LIBADD = $(switch_builddir)/libfreeswitch.la -lv8 -licuuc -licui18n +AM_LDFLAGS += -avoid-version -module -no-undefined -shared +AM_LDFLAGS += -L$(V8_DIR)/out/native/lib.target +BUILT_SOURCES = $(V8SO) $(LIBCURL_DEPS) + +if HAVE_ODBC +AM_CFLAGS += -DHAVE_ODBC $(ODBC_INC_FLAGS) +AM_CPPFLAGS += -DHAVE_ODBC $(ODBC_INC_FLAGS) +AM_LDFLAGS += -DHAVE_ODBC +endif + +mod_LTLIBRARIES = mod_v8.la +mod_v8_la_SOURCES = mod_v8.cpp ./src/fscoredb.cpp ./src/fscurl.cpp ./src/fsdtmf.cpp ./src/fsevent.cpp ./src/fsfileio.cpp ./src/fsglobal.cpp ./src/fsodbc.cpp ./src/fspcre.cpp ./src/fsrequest.cpp ./src/fssession.cpp ./src/fssocket.cpp ./src/fsteletone.cpp ./src/fsxml.cpp ./src/fsfile.cpp ./src/jsbase.cpp ./src/jsmain.cpp +mod_v8_la_CFLAGS = $(AM_CFLAGS) $(LIBCURL_CPPFLAGS) -I$(switch_srcdir)/libs/libteletone/src +mod_v8_la_CPPFLAGS = $(AM_CPPFLAGS) $(LIBCURL_CPPFLAGS) -I$(switch_srcdir)/libs/libteletone/src +mod_v8_la_LIBADD = $(AM_LIBADD) +mod_v8_la_LDFLAGS = $(AM_LDFLAGS) $(LIBCURL) + +#mod_LTLIBRARIES += mod_v8_skel.la +#mod_v8_skel_la_SOURCES = mod_v8_skel.cpp +#mod_v8_skel_la_CFLAGS = $(AM_CFLAGS) -DJSMOD_IMPORT +#mod_v8_skel_la_CPPFLAGS = $(AM_CPPFLAGS) -DJSMOD_IMPORT +#mod_v8_skel_la_LIBADD = $(AM_LIBADD) +#mod_v8_skel_la_LDFLAGS = $(AM_LDFLAGS) + +$(SOURCES): $(BUILT_SOURCES) + +$(V8_DIR): + $(GETLIB) $(V8).tar.bz2 + +$(V8SO): $(V8_DIR) + cd $(V8_BUILDDIR) && $(MAKE) library=shared native + +$(CURLLA): + @cd $(CURL_BUILDDIR) && $(MAKE) + $(TOUCH_TARGET) + +install-exec-local: $(DESTDIR)$(libdir)/libv8.so + +$(DESTDIR)$(libdir)/libv8.so: $(V8SO) + rm -f $(DESTDIR)$(libdir)/libv8.so && cp -a $(V8_DIR)/out/native/lib.target/libv8.so $(DESTDIR)$(libdir)/libv8.so + rm -f $(DESTDIR)$(libdir)/libicuuc.so && cp -a $(V8_DIR)/out/native/lib.target/libicuuc.so $(DESTDIR)$(libdir)/libicuuc.so + rm -f $(DESTDIR)$(libdir)/libicui18n.so && cp -a $(V8_DIR)/out/native/lib.target/libicui18n.so $(DESTDIR)$(libdir)/libicui18n.so diff --git a/src/mod/languages/mod_v8/conf/autoload_configs/v8.conf.xml b/src/mod/languages/mod_v8/conf/autoload_configs/v8.conf.xml new file mode 100644 index 0000000000..01c9e2bd99 --- /dev/null +++ b/src/mod/languages/mod_v8/conf/autoload_configs/v8.conf.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/src/mod/languages/mod_v8/include/fscoredb.hpp b/src/mod/languages/mod_v8/include/fscoredb.hpp new file mode 100644 index 0000000000..5ac0a052b5 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fscoredb.hpp @@ -0,0 +1,89 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fscoredb.hpp -- JavaScript CoreDB class header + * + */ + +#ifndef FS_COREDB_H +#define FS_COREDB_H + +#include "mod_v8.h" + +/* Macros for easier V8 callback definitions */ +#define JS_COREDB_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSCoreDB) +#define JS_COREDB_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSCoreDB) +#define JS_COREDB_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSCoreDB) +#define JS_COREDB_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSCoreDB) +#define JS_COREDB_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSCoreDB) +#define JS_COREDB_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSCoreDB) + +class FSCoreDB : public JSBase +{ +private: + switch_memory_pool_t *_pool; + switch_core_db_t *_db; + switch_core_db_stmt_t *_stmt; + const char *_dbname; + v8::Persistent _callback; + + void Init(); + void DoClose(); + void StepEx(const v8::FunctionCallbackInfo& info, int stepSuccessCode); + static int Callback(void *pArg, int argc, char **argv, char **columnNames); +public: + FSCoreDB(JSMain *owner) : JSBase(owner) { Init(); } + FSCoreDB(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSCoreDB(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_COREDB_FUNCTION_DEF(Exec); + JS_COREDB_FUNCTION_DEF(Close); + JS_COREDB_FUNCTION_DEF(Next); + JS_COREDB_FUNCTION_DEF(Step); + JS_COREDB_FUNCTION_DEF(Fetch); + JS_COREDB_FUNCTION_DEF(Prepare); + JS_COREDB_FUNCTION_DEF(BindText); + JS_COREDB_FUNCTION_DEF(BindInt); + JS_COREDB_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_COREDB_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fscurl.hpp b/src/mod/languages/mod_v8/include/fscurl.hpp new file mode 100644 index 0000000000..0a01d264d4 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fscurl.hpp @@ -0,0 +1,79 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fscurl.hpp -- JavaScript CURL class header + * + */ + +#ifndef FS_CURL_H +#define FS_CURL_H + +#include "mod_v8.h" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_CURL_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSCURL) +#define JS_CURL_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSCURL) +#define JS_CURL_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSCURL) +#define JS_CURL_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSCURL) +#define JS_CURL_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSCURL) +#define JS_CURL_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSCURL) + +class FSCURL : public JSBase +{ +private: + switch_CURL *_curl_handle; + v8::Persistent _function; + v8::Persistent _user_data; + v8::Persistent _ret; + + void Init(void); + static size_t FileCallback(void *ptr, size_t size, size_t nmemb, void *data); +public: + FSCURL(JSMain *owner) : JSBase(owner) { Init(); } + FSCURL(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSCURL(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_CURL_FUNCTION_DEF(Run); +}; + +#endif /* FS_CURL_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsdtmf.hpp b/src/mod/languages/mod_v8/include/fsdtmf.hpp new file mode 100644 index 0000000000..c931390cea --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsdtmf.hpp @@ -0,0 +1,74 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsdtmf.hpp -- JavaScript DTMF class header + * + */ + +#ifndef FS_DTMF_H +#define FS_DTMF_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_DTMF_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSDTMF) +#define JS_DTMF_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSDTMF) +#define JS_DTMF_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSDTMF) +#define JS_DTMF_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSDTMF) +#define JS_DTMF_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSDTMF) +#define JS_DTMF_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSDTMF) + +class FSDTMF : public JSBase +{ +private: + switch_dtmf_t *_dtmf; +public: + FSDTMF(JSMain *owner) : JSBase(owner) { _dtmf = NULL; } + FSDTMF(const v8::FunctionCallbackInfo& info) : JSBase(info) { _dtmf = NULL; } + virtual ~FSDTMF(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + static v8::Handle New(switch_dtmf_t *dtmf, const char *name, JSMain *js); + JS_DTMF_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_DTMF_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsevent.hpp b/src/mod/languages/mod_v8/include/fsevent.hpp new file mode 100644 index 0000000000..cc355b97ff --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsevent.hpp @@ -0,0 +1,93 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsevent.hpp -- JavaScript Event class header + * + */ + +#ifndef FS_EVENT_H +#define FS_EVENT_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_EVENT_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSEvent) +#define JS_EVENT_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSEvent) +#define JS_EVENT_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSEvent) +#define JS_EVENT_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSEvent) +#define JS_EVENT_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSEvent) +#define JS_EVENT_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSEvent) + +#define JS_EVENT_GET_PROPERTY_IMPL_STATIC(method_name) JS_GET_PROPERTY_IMPL_STATIC(method_name, FSEvent) +#define JS_EVENT_SET_PROPERTY_IMPL_STATIC(method_name) JS_SET_PROPERTY_IMPL_STATIC(method_name, FSEvent) +#define JS_EVENT_FUNCTION_IMPL_STATIC(method_name) JS_FUNCTION_IMPL_STATIC(method_name, FSEvent) + +class FSEvent : public JSBase +{ +private: + switch_event_t *_event; + int _freed; + + void Init(); +public: + FSEvent(JSMain *owner) : JSBase(owner) { Init(); } + FSEvent(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSEvent(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + void SetEvent(switch_event_t *event, int freed = 0); + switch_event_t **GetEvent(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + static v8::Handle New(switch_event_t *event, const char *name, JSMain *js); + JS_EVENT_FUNCTION_DEF(AddHeader); + JS_EVENT_FUNCTION_DEF(GetHeader); + JS_EVENT_FUNCTION_DEF(AddBody); + JS_EVENT_FUNCTION_DEF(GetBody); + JS_EVENT_FUNCTION_DEF(GetType); + JS_EVENT_FUNCTION_DEF(Serialize); + JS_EVENT_FUNCTION_DEF(ChatExecute); + JS_FUNCTION_DEF_STATIC(Fire); // This will also destroy the C++ object + JS_FUNCTION_DEF_STATIC(Destroy); // This will also destroy the C++ object + JS_EVENT_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_EVENT_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsfile.hpp b/src/mod/languages/mod_v8/include/fsfile.hpp new file mode 100644 index 0000000000..7f98e13b5e --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsfile.hpp @@ -0,0 +1,111 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsfile.hpp -- JavaScript File class header + * + */ + +#ifndef FS_FILE_H +#define FS_FILE_H + +#include "mod_v8.h" + +/* Macros for easier V8 callback definitions */ +#define JS_FILE_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSFile) +#define JS_FILE_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSFile) +#define JS_FILE_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSFile) +#define JS_FILE_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSFile) +#define JS_FILE_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSFile) +#define JS_FILE_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSFile) + +class FSFile : public JSBase +{ +private: + void Init(); +public: + FSFile(JSMain *owner) : JSBase(owner) { } + FSFile(const v8::FunctionCallbackInfo& info) : JSBase(info) { } + virtual ~FSFile(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_FILE_FUNCTION_DEF(Close); + JS_FILE_FUNCTION_DEF(CopyTo); + JS_FILE_FUNCTION_DEF(Flush); + JS_FILE_FUNCTION_DEF(List); + JS_FILE_FUNCTION_DEF(MkDir); + JS_FILE_FUNCTION_DEF(Open); + JS_FILE_FUNCTION_DEF(Read); + JS_FILE_FUNCTION_DEF(ReadAll); + JS_FILE_FUNCTION_DEF(ReadLn); + JS_FILE_FUNCTION_DEF(Remove); + JS_FILE_FUNCTION_DEF(RenameTo); + JS_FILE_FUNCTION_DEF(Seek); + JS_FILE_FUNCTION_DEF(ToString); + JS_FILE_FUNCTION_DEF(ToURL); + JS_FILE_FUNCTION_DEF(Write); + JS_FILE_FUNCTION_DEF(WriteAll); + JS_FILE_FUNCTION_DEF(WriteLn); + + JS_FILE_GET_PROPERTY_DEF(GetPropCanAppend); + JS_FILE_GET_PROPERTY_DEF(GetPropCanRead); + JS_FILE_GET_PROPERTY_DEF(GetPropCanReplace); + JS_FILE_GET_PROPERTY_DEF(GetPropCanWrite); + JS_FILE_GET_PROPERTY_DEF(GetPropCreationTime); + JS_FILE_GET_PROPERTY_DEF(GetPropExists); + JS_FILE_GET_PROPERTY_DEF(GetPropHasAutoFlush); + JS_FILE_GET_PROPERTY_DEF(GetPropHasRandomAccess); + JS_FILE_GET_PROPERTY_DEF(GetPropIsDirectory); + JS_FILE_GET_PROPERTY_DEF(GetPropIsFile); + JS_FILE_GET_PROPERTY_DEF(GetPropIsNative); + JS_FILE_GET_PROPERTY_DEF(GetPropIsOpen); + JS_FILE_GET_PROPERTY_DEF(GetPropLastModified); + JS_FILE_GET_PROPERTY_DEF(GetPropLength); + JS_FILE_GET_PROPERTY_DEF(GetPropMode); + JS_FILE_GET_PROPERTY_DEF(GetPropName); + JS_FILE_GET_PROPERTY_DEF(GetPropParent); + JS_FILE_GET_PROPERTY_DEF(GetPropPath); + JS_FILE_GET_PROPERTY_DEF(GetPropPosition); + JS_FILE_SET_PROPERTY_DEF(SetPropPosition); + JS_FILE_GET_PROPERTY_DEF(GetPropSize); + JS_FILE_GET_PROPERTY_DEF(GetPropType); +}; + +#endif /* FS_FILE_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsfileio.hpp b/src/mod/languages/mod_v8/include/fsfileio.hpp new file mode 100644 index 0000000000..05a4d220db --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsfileio.hpp @@ -0,0 +1,84 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsfileio.hpp -- JavaScript FileIO class header + * + */ + +#ifndef FS_FILEIO_H +#define FS_FILEIO_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_FILEIO_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSFileIO) +#define JS_FILEIO_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSFileIO) +#define JS_FILEIO_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSFileIO) +#define JS_FILEIO_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSFileIO) +#define JS_FILEIO_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSFileIO) +#define JS_FILEIO_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSFileIO) + +class FSFileIO : public JSBase +{ +private: + char *_path; + unsigned int _flags; + switch_file_t *_fd; + switch_memory_pool_t *_pool; + char *_buf; + switch_size_t _buflen; + int32_t _bufsize; + + void Init(); +public: + FSFileIO(JSMain *owner) : JSBase(owner) { Init(); } + FSFileIO(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSFileIO(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_FILEIO_FUNCTION_DEF(Read); + JS_FILEIO_FUNCTION_DEF(Write); + JS_FILEIO_FUNCTION_DEF(GetData); + JS_FILEIO_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_FILEIO_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsglobal.hpp b/src/mod/languages/mod_v8/include/fsglobal.hpp new file mode 100644 index 0000000000..7095a12dd2 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsglobal.hpp @@ -0,0 +1,79 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsglobal.hpp -- JavaScript Global functions + * + */ + +#ifndef FS_GLOBAL_H +#define FS_GLOBAL_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_GLOBAL_FUNCTION_IMPL_STATIC(method_name) JS_FUNCTION_IMPL_STATIC(method_name, FSGlobal) + +/* All globally available functions (not related to objects) */ +class FSGlobal +{ +private: + static size_t HashCallback(void *ptr, size_t size, size_t nmemb, void *data); + static size_t FileCallback(void *ptr, size_t size, size_t nmemb, void *data); + static size_t FetchUrlCallback(void *ptr, size_t size, size_t nmemb, void *data); +public: + static const js_function_t *GetFunctionDefinitions(); + + /* Methods available from JavaScript */ + JS_FUNCTION_DEF_STATIC(Log); + JS_FUNCTION_DEF_STATIC(GlobalGet); + JS_FUNCTION_DEF_STATIC(GlobalSet); + JS_FUNCTION_DEF_STATIC(Exit); + JS_FUNCTION_DEF_STATIC(Include); + JS_FUNCTION_DEF_STATIC(Bridge); + JS_FUNCTION_DEF_STATIC(Email); + JS_FUNCTION_DEF_STATIC(ApiExecute); + JS_FUNCTION_DEF_STATIC(Use); + JS_FUNCTION_DEF_STATIC(Sleep); + JS_FUNCTION_DEF_STATIC(FileDelete); + JS_FUNCTION_DEF_STATIC(System); + JS_FUNCTION_DEF_STATIC(FetchURL); + JS_FUNCTION_DEF_STATIC(FetchURLHash); + JS_FUNCTION_DEF_STATIC(FetchURLFile); +}; + +#endif /* FS_GLOBAL_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsodbc.hpp b/src/mod/languages/mod_v8/include/fsodbc.hpp new file mode 100644 index 0000000000..c3e6b4e153 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsodbc.hpp @@ -0,0 +1,114 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsodbc.hpp -- JavaScript ODBC class header + * + */ + +#ifndef FS_ODBC_H +#define FS_ODBC_H + +#include "mod_v8.h" + +#if defined(WIN32) && !defined(HAVE_ODBC) +#define HAVE_ODBC +#endif + +#ifdef HAVE_ODBC + +#include +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4201) +#include +#pragma warning(pop) +#else +#include +#endif +#include + +/* Macros for easier V8 callback definitions */ +#define JS_ODBC_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSODBC) +#define JS_ODBC_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSODBC) +#define JS_ODBC_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSODBC) +#define JS_ODBC_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSODBC) +#define JS_ODBC_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSODBC) +#define JS_ODBC_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSODBC) + +#define JS_ODBC_GET_PROPERTY_IMPL_STATIC(method_name) JS_GET_PROPERTY_IMPL_STATIC(method_name, FSODBC) +#define JS_ODBC_SET_PROPERTY_IMPL_STATIC(method_name) JS_SET_PROPERTY_IMPL_STATIC(method_name, FSODBC) +#define JS_ODBC_FUNCTION_IMPL_STATIC(method_name) JS_FUNCTION_IMPL_STATIC(method_name, FSODBC) + +class FSODBC : public JSBase +{ +private: + switch_odbc_handle_t *_handle; + SQLHSTMT _stmt; + SQLCHAR *_colbuf; + int32_t _cblen; + std::string _dsn; + + void Init(); +public: + FSODBC(JSMain *owner) : JSBase(owner) { Init(); } + FSODBC(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSODBC(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + static FSODBC *New(char *dsn, char *username, char *password, const v8::FunctionCallbackInfo& info); + switch_odbc_status_t Connect(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_ODBC_FUNCTION_DEF(Connect); + JS_ODBC_FUNCTION_DEF(Disconnect); + JS_ODBC_FUNCTION_DEF(Exec); + JS_ODBC_FUNCTION_DEF(Execute); + JS_ODBC_FUNCTION_DEF(NumRows); + JS_ODBC_FUNCTION_DEF(NumCols); + JS_ODBC_FUNCTION_DEF(NextRow); + JS_ODBC_FUNCTION_DEF(GetData); + JS_FUNCTION_DEF_STATIC(Close); // This will also destroy the C++ object + JS_ODBC_GET_PROPERTY_DEF(GetProperty); + +}; + +#endif /* HAVE_ODBC */ + +#endif /* FS_ODBC_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fspcre.hpp b/src/mod/languages/mod_v8/include/fspcre.hpp new file mode 100644 index 0000000000..da1a3bbe2d --- /dev/null +++ b/src/mod/languages/mod_v8/include/fspcre.hpp @@ -0,0 +1,81 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fspcre.hpp -- JavaScript PCRE class header + * + */ + +#ifndef FS_PCRE_H +#define FS_PCRE_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_PCRE_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSPCRE) +#define JS_PCRE_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSPCRE) +#define JS_PCRE_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSPCRE) +#define JS_PCRE_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSPCRE) +#define JS_PCRE_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSPCRE) +#define JS_PCRE_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSPCRE) + +class FSPCRE : public JSBase +{ +private: + switch_regex_t *_re; + char *_str; + int _proceed; + int _ovector[30]; + int _freed; + + void Init(); +public: + FSPCRE(JSMain *owner) : JSBase(owner) { Init(); } + FSPCRE(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSPCRE(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_PCRE_FUNCTION_DEF(Compile); + JS_PCRE_FUNCTION_DEF(Substitute); + JS_PCRE_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_PCRE_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsrequest.hpp b/src/mod/languages/mod_v8/include/fsrequest.hpp new file mode 100644 index 0000000000..c96bb8824b --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsrequest.hpp @@ -0,0 +1,81 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsrequest.hpp -- JavaScript Request class header + * + */ + +#ifndef FS_REQUEST_H +#define FS_REQUEST_H + +#include "javascript.hpp" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_REQUEST_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSRequest) +#define JS_REQUEST_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSRequest) +#define JS_REQUEST_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSRequest) +#define JS_REQUEST_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSRequest) +#define JS_REQUEST_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSRequest) +#define JS_REQUEST_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSRequest) + +class FSRequest : public JSBase +{ +private: + const char *_cmd; + switch_stream_handle_t *_stream; + + void Init(); +public: + FSRequest(JSMain *owner) : JSBase(owner) { Init(); } + FSRequest(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSRequest(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + void Init(const char *cmd, switch_stream_handle_t *stream); + + /* Methods available from JavaScript */ + JS_REQUEST_FUNCTION_DEF(Write); + JS_REQUEST_FUNCTION_DEF(GetHeader); + JS_REQUEST_FUNCTION_DEF(AddHeader); + JS_REQUEST_FUNCTION_DEF(DumpEnv); + JS_REQUEST_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_REQUEST_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fssession.hpp b/src/mod/languages/mod_v8/include/fssession.hpp new file mode 100644 index 0000000000..aa918534b5 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fssession.hpp @@ -0,0 +1,165 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fssession.hpp -- JavaScript Session class header + * + */ + +#ifndef FS_SESSION_H +#define FS_SESSION_H + +#include "javascript.hpp" +#include + +typedef struct { + switch_speech_handle_t sh; + switch_codec_t codec; + int speaking; +} js_session_speech_t; + +typedef enum { + S_HUP = (1 << 0), +} session_flag_t; + +/* Macros for easier V8 callback definitions */ +#define JS_SESSION_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSSession) +#define JS_SESSION_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSSession) +#define JS_SESSION_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSSession) +#define JS_SESSION_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSSession) +#define JS_SESSION_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSSession) +#define JS_SESSION_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSSession) + +class FSSession : public JSBase +{ +private: + switch_core_session_t *_session; // The FS session + unsigned int flags; // Flags related to this session + switch_call_cause_t _cause; // Hangup cause + v8::Persistent _on_hangup;// Hangup hook + int _stack_depth; + switch_channel_state_t _hook_state; + char *_destination_number; + char *_dialplan; + char *_caller_id_name; + char *_caller_id_number; + char *_network_addr; + char *_ani; + char *_aniii; + char *_rdnis; + char *_context; + char *_username; + int _check_state; + js_session_speech_t *_speech; + + void Init(void); + switch_status_t InitSpeechEngine(const char *engine, const char *voice); + void DestroySpeechEngine(); + static switch_status_t CommonCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); + static switch_status_t StreamInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); + static switch_status_t RecordInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); + static bool CheckHangupHook(FSSession *obj, bool *ret); + static switch_status_t HangupHook(switch_core_session_t *session); +public: + FSSession(JSMain *owner) : JSBase(owner) { Init(); } + FSSession(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSSession(void); + virtual std::string GetJSClassName(); + + static const js_class_definition_t *GetClassDefinition(); + + switch_core_session_t *GetSession(); + void Init(switch_core_session_t *session, unsigned int flags); + static switch_status_t CollectInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_SESSION_FUNCTION_DEF(Originate); + JS_SESSION_FUNCTION_DEF(SetCallerdata); + JS_SESSION_FUNCTION_DEF(SetHangupHook); + JS_SESSION_FUNCTION_DEF(SetAutoHangup); + JS_SESSION_FUNCTION_DEF(SayPhrase); + JS_SESSION_FUNCTION_DEF(StreamFile); + JS_SESSION_FUNCTION_DEF(CollectInput); + JS_SESSION_FUNCTION_DEF(RecordFile); + JS_SESSION_FUNCTION_DEF(FlushEvents); + JS_SESSION_FUNCTION_DEF(FlushDigits); + JS_SESSION_FUNCTION_DEF(Speak); + JS_SESSION_FUNCTION_DEF(SetVariable); + JS_SESSION_FUNCTION_DEF(GetVariable); + JS_SESSION_FUNCTION_DEF(GetDigits); + JS_SESSION_FUNCTION_DEF(Answer); + JS_SESSION_FUNCTION_DEF(PreAnswer); + JS_SESSION_FUNCTION_DEF(GenerateXmlCdr); + JS_SESSION_FUNCTION_DEF(Ready); + JS_SESSION_FUNCTION_DEF(Answered); + JS_SESSION_FUNCTION_DEF(MediaReady); + JS_SESSION_FUNCTION_DEF(RingReady); + JS_SESSION_FUNCTION_DEF(WaitForAnswer); + JS_SESSION_FUNCTION_DEF(WaitForMedia); + JS_SESSION_FUNCTION_DEF(GetEvent); + JS_SESSION_FUNCTION_DEF(SendEvent); + JS_SESSION_FUNCTION_DEF(Hangup); + JS_SESSION_FUNCTION_DEF(Execute); + JS_SESSION_FUNCTION_DEF(Detach); + JS_SESSION_FUNCTION_DEF(Sleep); + JS_SESSION_FUNCTION_DEF(Bridge); + JS_SESSION_GET_PROPERTY_DEF(GetProperty); +}; + +class FSInputCallbackState +{ +public: + FSSession *session_state; + char code_buffer[1024]; + size_t code_buffer_len; + char ret_buffer[1024]; + int ret_buffer_len; + int digit_count; + v8::Persistent function; + v8::Persistent arg; + v8::Persistent ret; + void *extra; + FSSession *jss_a; + FSSession *jss_b; + v8::Persistent session_obj_a; + v8::Persistent session_obj_b; + + FSInputCallbackState(void); + ~FSInputCallbackState(void); +}; + +#endif /* FS_SESSION_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fssocket.hpp b/src/mod/languages/mod_v8/include/fssocket.hpp new file mode 100644 index 0000000000..58f4bb5838 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fssocket.hpp @@ -0,0 +1,83 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fssocket.hpp -- JavaScript Socket class header + * + */ + +#ifndef FS_SOCKET_H +#define FS_SOCKET_H + +#include "mod_v8.h" + +/* Macros for easier V8 callback definitions */ +#define JS_SOCKET_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSSocket) +#define JS_SOCKET_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSSocket) +#define JS_SOCKET_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSSocket) +#define JS_SOCKET_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSSocket) +#define JS_SOCKET_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSSocket) +#define JS_SOCKET_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSSocket) + +class FSSocket : public JSBase +{ +private: + switch_socket_t *_socket; + switch_memory_pool_t *_pool; + char *_read_buffer; + switch_size_t _buffer_size; + int _state; + + void Init(); +public: + FSSocket(JSMain *owner) : JSBase(owner) { Init(); } + FSSocket(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSSocket(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_SOCKET_FUNCTION_DEF(Connect); + JS_SOCKET_FUNCTION_DEF(Close); + JS_SOCKET_FUNCTION_DEF(Send); + JS_SOCKET_FUNCTION_DEF(ReadBytes); + JS_SOCKET_FUNCTION_DEF(Read); + JS_SOCKET_GET_PROPERTY_DEF(GetProperty); +}; + +#endif /* FS_SOCKET_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsteletone.hpp b/src/mod/languages/mod_v8/include/fsteletone.hpp new file mode 100644 index 0000000000..0375b3a75b --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsteletone.hpp @@ -0,0 +1,89 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsteletone.hpp -- JavaScript TeleTone class header + * + */ + +#ifndef FS_TELETONE_H +#define FS_TELETONE_H + +#include "mod_v8.h" +#include + +/* Macros for easier V8 callback definitions */ +#define JS_TELETONE_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSTeleTone) +#define JS_TELETONE_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSTeleTone) +#define JS_TELETONE_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSTeleTone) +#define JS_TELETONE_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSTeleTone) +#define JS_TELETONE_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSTeleTone) +#define JS_TELETONE_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSTeleTone) + +class FSTeleTone : public JSBase +{ +private: + teletone_generation_session_t _ts; + switch_core_session_t *_session; + switch_codec_t _codec; + switch_buffer_t *_audio_buffer; + switch_memory_pool_t *_pool; + switch_timer_t *_timer; + switch_timer_t _timer_base; + v8::Persistent _function; + v8::Persistent _arg; + unsigned int flags; + + void Init(); +public: + FSTeleTone(JSMain *owner) : JSBase(owner) { Init(); } + FSTeleTone(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSTeleTone(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + static int Handler(teletone_generation_session_t *ts, teletone_tone_map_t *map); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_TELETONE_FUNCTION_DEF(Generate); + JS_TELETONE_FUNCTION_DEF(OnDTMF); + JS_TELETONE_FUNCTION_DEF(AddTone); + JS_TELETONE_GET_PROPERTY_DEF(GetNameProperty); +}; + +#endif /* FS_TELETONE_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/fsxml.hpp b/src/mod/languages/mod_v8/include/fsxml.hpp new file mode 100644 index 0000000000..fb806ce653 --- /dev/null +++ b/src/mod/languages/mod_v8/include/fsxml.hpp @@ -0,0 +1,96 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * fsxml.hpp -- JavaScript XML class header + * + */ + +#ifndef FS_XML_H +#define FS_XML_H + +#include "mod_v8.h" + +/* Macros for easier V8 callback definitions */ +#define JS_XML_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSXML) +#define JS_XML_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSXML) +#define JS_XML_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSXML) +#define JS_XML_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSXML) +#define JS_XML_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSXML) +#define JS_XML_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSXML) +#define JS_XML_IMPL_STATIC(method_name) JS_FUNCTION_IMPL_STATIC(method_name, FSXML) + +class FSXML : public JSBase +{ +private: + switch_xml_t _xml; + v8::Persistent _rootJSObject; /* Always keep a reference to the root, so JS doesn't try to clean it up in GC */ + FSXML *_rootObject; + switch_hash_t *_obj_hash; + switch_memory_pool_t *_pool; + + void Init(); + void InitRootObject(); + v8::Handle GetJSObjFromXMLObj(const switch_xml_t xml, const v8::FunctionCallbackInfo& info); + void StoreObjectInHash(switch_xml_t xml, FSXML *obj); + FSXML *FindObjectInHash(switch_xml_t xml); + void DeleteObjectInHash(switch_xml_t xml); + void DestroyHash(); +public: + FSXML(JSMain *owner) : JSBase(owner) { Init(); } + FSXML(const v8::FunctionCallbackInfo& info) : JSBase(info) { Init(); } + virtual ~FSXML(void); + virtual std::string GetJSClassName(); + + static const v8_mod_interface_t *GetModuleInterface(); + + /* Methods available from JavaScript */ + static void *Construct(const v8::FunctionCallbackInfo& info); + JS_XML_FUNCTION_DEF(GetChild); + JS_XML_FUNCTION_DEF(AddChild); + JS_XML_FUNCTION_DEF(Next); + JS_XML_FUNCTION_DEF(GetAttribute); + JS_XML_FUNCTION_DEF(SetAttribute); + JS_FUNCTION_DEF_STATIC(Remove); + JS_XML_FUNCTION_DEF(Copy); + JS_XML_FUNCTION_DEF(Serialize); + JS_XML_GET_PROPERTY_DEF(GetNameProperty); + JS_XML_GET_PROPERTY_DEF(GetDataProperty); + JS_XML_SET_PROPERTY_DEF(SetDataProperty); + JS_XML_GET_PROPERTY_DEF(GetXmlErrorProperty); +}; + +#endif /* FS_XML_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/include/javascript.hpp b/src/mod/languages/mod_v8/include/javascript.hpp new file mode 100644 index 0000000000..a465bf6ea9 --- /dev/null +++ b/src/mod/languages/mod_v8/include/javascript.hpp @@ -0,0 +1,356 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Peter Olsson + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * + * javascript.hpp -- Header file for main JavaScript classes + * + */ + +#ifndef V8_JAVASCRIPT_H +#define V8_JAVASCRIPT_H + +#include +#include + +#include +#include +#include +#include + +/* Enable this define enable V8 debugging protocol, this is not yet working */ +//#define V8_ENABLE_DEBUGGING + +/* + * Enable this define to force a GC after the script has finished execution. + * This is only to help debug memory leaks, and should not be needed for anything else + */ +//#define V8_FORCE_GC_AFTER_EXECUTION + + +/* Macro for easy V8 "get property" callback definition */ +#define JS_GET_PROPERTY_DEF(method_name, class_name) \ + static void method_name(v8::Local property, const v8::PropertyCallbackInfo& info)\ + {\ + JS_CHECK_SCRIPT_STATE();\ + class_name *obj = JSBase::GetInstance(info.Holder());\ + if (obj) {\ + obj->method_name##Impl(property, info);\ + } else {\ + int line;\ + char *file = JSMain::GetStackInfo(info.GetIsolate(), &line);\ + v8::String::Utf8Value str(info.Holder());\ + switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "mod_v8", line, NULL, SWITCH_LOG_DEBUG, "No valid internal data available for %s when calling %s\n", *str ? *str : "[unknown]", #class_name "::" #method_name "()");\ + free(file);\ + info.GetReturnValue().Set(false);\ + }\ + }\ + void method_name##Impl(v8::Local property, const v8::PropertyCallbackInfo& info) + +/* Macro for easy V8 "set property" callback definition */ +#define JS_SET_PROPERTY_DEF(method_name, class_name) \ + static void method_name(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info)\ + {\ + JS_CHECK_SCRIPT_STATE();\ + class_name *obj = JSBase::GetInstance(info.Holder());\ + if (obj) {\ + obj->method_name##Impl(property, value, info);\ + } else {\ + int line;\ + char *file = JSMain::GetStackInfo(info.GetIsolate(), &line);\ + v8::String::Utf8Value str(info.Holder());\ + switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "mod_v8", line, NULL, SWITCH_LOG_DEBUG, "No valid internal data available for %s when calling %s\n", *str ? *str : "[unknown]", #class_name "::" #method_name "()");\ + free(file);\ + info.GetReturnValue().Set(false);\ + }\ + }\ + void method_name##Impl(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) + +/* Macro for easy V8 "function" callback definition */ +#define JS_FUNCTION_DEF(method_name, class_name) \ + static void method_name(const v8::FunctionCallbackInfo& info)\ + {\ + JS_CHECK_SCRIPT_STATE();\ + class_name *obj = JSBase::GetInstance(info.Holder());\ + if (obj) {\ + obj->method_name##Impl(info);\ + } else {\ + int line;\ + char *file = JSMain::GetStackInfo(info.GetIsolate(), &line);\ + v8::String::Utf8Value str(info.Holder());\ + switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "mod_v8", line, NULL, SWITCH_LOG_DEBUG, "No valid internal data available for %s when calling %s\n", *str ? *str : "[unknown]", #class_name "::" #method_name "()");\ + free(file);\ + info.GetReturnValue().Set(false);\ + }\ + }\ + void method_name##Impl(const v8::FunctionCallbackInfo& info) + +/* Macros for V8 callback implementations */ +#define JS_GET_PROPERTY_IMPL(method_name, class_name) void class_name::method_name##Impl(v8::Local property, const v8::PropertyCallbackInfo& info) +#define JS_SET_PROPERTY_IMPL(method_name, class_name) void class_name::method_name##Impl(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) +#define JS_FUNCTION_IMPL(method_name, class_name) void class_name::method_name##Impl(const v8::FunctionCallbackInfo& info) + +/* Macros for V8 callback definitions (class static version) */ +#define JS_GET_PROPERTY_DEF_STATIC(method_name) static void method_name(v8::Local property, const v8::PropertyCallbackInfo& info) +#define JS_SET_PROPERTY_DEF_STATIC(method_name) static void method_name(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) +#define JS_FUNCTION_DEF_STATIC(method_name) static void method_name(const v8::FunctionCallbackInfo& info) + +/* Macros for V8 callback implementations (class static version) */ +#define JS_GET_PROPERTY_IMPL_STATIC(method_name, class_name) void class_name::method_name(v8::Local property, const v8::PropertyCallbackInfo& info) +#define JS_SET_PROPERTY_IMPL_STATIC(method_name, class_name) void class_name::method_name(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) +#define JS_FUNCTION_IMPL_STATIC(method_name, class_name) void class_name::method_name(const v8::FunctionCallbackInfo& info) + +/* Macro for basic script state check (to know if the script is being terminated), should be called before calling any callback actual code */ +#define JS_CHECK_SCRIPT_STATE() \ + if (v8::V8::IsExecutionTerminating(info.GetIsolate())) return;\ + if (JSMain::GetScriptInstanceFromIsolate(info.GetIsolate()) && JSMain::GetScriptInstanceFromIsolate(info.GetIsolate())->GetForcedTermination()) return + +/* strdup function for all platforms */ +#ifdef NDEBUG +#if (_MSC_VER >= 1500) // VC9+ +#define js_strdup(ptr, s) (void)( (!!(ptr = _strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr) +#else +#define js_strdup(ptr, s) (void)( (!!(ptr = strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr) +#endif +#else +#if (_MSC_VER >= 1500) // VC9+ +#define js_strdup(ptr, s) (void)(assert(((ptr) = _strdup(s))),ptr);__analysis_assume( ptr ) +#else +#define js_strdup(ptr, s) (void)(assert(((ptr) = strdup((s)))),ptr) +#endif +#endif + +/* Makes sure to return a valid char pointer */ +#define js_safe_str(s) (s ? s : "") + +/* JS Constructor callback definition */ +typedef void * void_pointer_t; +typedef void_pointer_t (*ConstructorCallback)(const v8::FunctionCallbackInfo& info); + +/* JS Function definition */ +typedef struct { + const char *name; /* Name of the function */ + v8::FunctionCallback func; /* Function callback */ +} js_function_t; + +/* JS Property definition */ +typedef struct { + const char *name; /* Name of the property */ + v8::AccessorGetterCallback get; /* The property getter */ + v8::AccessorSetterCallback set; /* The property setter */ +} js_property_t; + +/* JS Class definition */ +typedef struct { + const char *name; /* The name of the class */ + ConstructorCallback constructor; /* The constructor definition */ + const js_function_t *functions; /* An array of function definitions */ + const js_property_t *properties; /* An array of property definitions */ +} js_class_definition_t; + +/* Import/export definitions (used by extra loadable modules) */ +#ifdef WIN32 +/* WIN32 */ +#ifdef JSMOD_IMPORT +#define JSMOD_EXPORT __declspec(dllimport) +#else +#define JSMOD_EXPORT __declspec(dllexport) +#endif +#else +/* Not WIN32 */ +#ifdef JSMOD_IMPORT +#define JSMOD_EXPORT +#else +#if (HAVE_VISIBILITY != 1) +#define JSMOD_EXPORT +#else +#define JSMOD_EXPORT __attribute__ ((visibility("default"))) +#endif +#endif +#endif + +/* JSMain class prototype */ +class JSMOD_EXPORT JSMain; + +/* Base class used by all C++ classes implemented in JS */ +class JSMOD_EXPORT JSBase +{ +private: + v8::Persistent *persistentHandle; /* The persistent handle of the JavaScript object for this instance */ + bool autoDestroy; /* flag to tell if this instance should be auto destroyed during JavaScript GC */ + JSMain *js; /* The "owner" of this instance */ + + /* The callback that happens when the V8 GC cleans up object instances */ + static void WeakCallback(const v8::WeakCallbackData& data); + + /* Internal basic constructor when creating a new instance from JS. It will call the actual user code inside */ + static void CreateInstance(const v8::FunctionCallbackInfo& args); + + /* Store a C++ instance to a JS object's private data */ + static void AddInstance(v8::Isolate *isolate, const v8::Handle& handle, const v8::Handle& object, bool autoDestroy); +public: + JSBase(JSMain *owner); + JSBase(const v8::FunctionCallbackInfo& info); + virtual ~JSBase(void); + + /* Returns the JS object related to the C++ instance */ + v8::Handle GetJavaScriptObject(); + + /* Register a C++ class inside V8 (must be called within a entered isolate, and context) */ + static void Register(v8::Isolate *isolate, const js_class_definition_t *desc); + + /* Register an existing C++ class instance inside V8 (must be called within a entered isolate, and context) */ + void RegisterInstance(v8::Isolate *isolate, std::string name, bool autoDestroy); + + /* Get a JSBase instance from JavaScript callback arguments */ + template static T *GetInstance(const v8::FunctionCallbackInfo& info) + { + v8::HandleScope scope(info.GetIsolate()); + return GetInstance(info.Holder()); + } + + /* Get a JSBase instance from a JavaScript object */ + template static T *GetInstance(const v8::Local& self) + { + v8::Local val = self->GetInternalField(0); + + if (!val.IsEmpty() && val->IsExternal()) { + v8::Local wrap = v8::Local::Cast(val); + JSBase *ptr = static_cast(wrap->Value()); + return dynamic_cast(ptr); /* If we're trying to cast to the wrong type, dynamic_cast will return NULL */ + } else { + return NULL; + } + } + + /* Get a JavaScript function from a JavaScript argument (can be either a string or the actual function) */ + static v8::Handle GetFunctionFromArg(v8::Isolate *isolate, const v8::Local& arg); + + /* Default JS setter callback, to be used for read only values */ + static void DefaultSetProperty(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info); + + /* Get the name of the JavaScript class - must be overridden by the actual implementation */ + virtual std::string GetJSClassName() = 0; + + /* Get the JavaScript class instance that owns this instance */ + JSMain *GetOwner(); + + /* Get the JavaScript isolate that's active for the current context */ + v8::Isolate *GetIsolate(); + + /* Get autoDestroy variable */ + bool GetAutoDestroy(); +}; + +/* Definition of the class registration method */ +typedef void (*JSExtenderRegisterMethod)(js_class_definition_t *class_definition); + +/* The struct holding a C++ class instance, to be used in JS */ +typedef struct { + JSBase *obj; /* The class instance to be used in JS */ + char *name; /* The name of the instance within JS */ + bool auto_destroy; /* Flag to know if the instance should be auto destroyed when not needed by JS anymore */ +} registered_instance_t; + +/* Main class for executing a V8 JavaScript */ +class JSMOD_EXPORT JSMain +{ +private: + v8::Isolate* isolate; /* The V8 isolate for this script instance */ + + std::vector *extenderClasses;/* List holding C++ classes to be registered in JS on execution */ + std::vector *extenderFunctions; /* List holding C++ functions to be registered in JS on execution */ + std::vector *extenderInstances; /* List holding C++ class instances to be registered in JS on execution */ + std::set *activeInstances; /* List holding all active instances right now (in a running script) */ + + bool forcedTermination; /* Is set to true if script is triggering a forced termination of the script */ + char *forcedTerminationMessage; /* The message given during forced termination */ + int forcedTerminationLineNumber; /* The JS line number that called the exit function */ + char *forcedTerminationScriptFile; /* The JS script file that called the exit function */ + + /* Internal Log function accessable from JS - used just for testing */ + static void Log(const v8::FunctionCallbackInfo& args); +public: + JSMain(void); + ~JSMain(void); + + void AddJSExtenderFunction(v8::FunctionCallback func, const std::string& name); /* Add a C++ function to be registered when running the script */ + void AddJSExtenderClass(const js_class_definition_t *method); /* Add a C++ class to be registered when running the script */ + void AddJSExtenderInstance(JSBase *instance, const std::string& objectName, bool autoDestroy); /* Add a C++ class instance to be registered when running the script */ + + static JSMain *GetScriptInstanceFromIsolate(v8::Isolate* isolate); /* Get the JavaScript C++ instance from a V8 isolate */ + v8::Isolate *GetIsolate(); /* Get the V8 isolate from the current instance */ + + const std::string ExecuteScript(const std::string& filename, bool *resultIsError); + const std::string ExecuteString(const std::string& scriptData, const std::string& fileName, bool *resultIsError); + + static void Initialize(); /* Initialize the V8 engine */ + static void Dispose(); /* Deinitialize the V8 engine */ + + static void Include(const v8::FunctionCallbackInfo& args); /* Adds functionality to include another JavaScript from the running script */ + static void Version(const v8::FunctionCallbackInfo& args); /* Internal Version function accessable from JS - used to get the current V( version */ + static const std::string GetExceptionInfo(v8::Isolate* isolate, v8::TryCatch* try_catch); /* Get the exception information from a V8 TryCatch instance */ + + const std::vector& GetExtenderClasses() const;/* Returns the list of class definitions */ + const std::vector& GetExtenderFunctions() const; /* Returns the list of function definitions */ + const std::vector& GetExtenderInstances() const; /* Returns the list of class instance definitions */ + + /* Methods to keep track of all created C++ instances within JS */ + void AddActiveInstance(JSBase *obj); + void RemoveActiveInstance(JSBase *obj); + void DisposeActiveInstances(); + + static bool FileExists(const char *file); + static const std::string LoadFileToString(const std::string& filename); + + /* Data related to forced script termination */ + bool GetForcedTermination(void); + void ResetForcedTermination(void); + const char *GetForcedTerminationMessage(void); + const char *GetForcedTerminationScriptFile(void); + int GetForcedTerminationLineNumber(void); + + /* Method to force termination of a script */ + static void ExitScript(v8::Isolate *isolate, const char *msg); + + /* Get the filename and line number of the current JS stack */ + static char *GetStackInfo(v8::Isolate *isolate, int *lineNumber); +}; + +#ifdef V8_ENABLE_DEBUGGING +void V8DispatchDebugMessages(); +#endif + +#endif /* V8_JAVASCRIPT_H */ + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: + */ diff --git a/src/mod/languages/mod_v8/mod_v8.2012.vcxproj b/src/mod/languages/mod_v8/mod_v8.2012.vcxproj new file mode 100644 index 0000000000..3a5baa2d1b --- /dev/null +++ b/src/mod/languages/mod_v8/mod_v8.2012.vcxproj @@ -0,0 +1,225 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + mod_v8 + {9B9D2551-D6BD-4F20-8BE5-DE30E154A064} + mod_v8 + Win32Proj + + + + DynamicLibrary + MultiByte + v110 + + + DynamicLibrary + MultiByte + v110 + + + DynamicLibrary + MultiByte + v110 + + + DynamicLibrary + MultiByte + v110 + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + + + NativeMinimumRules.ruleset + false + + + NativeMinimumRules.ruleset + false + + + NativeMinimumRules.ruleset + false + + + NativeMinimumRules.ruleset + false + + + + Disabled + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + false + false + + + $(Platform)\$(Configuration)\$(ProjectName).lib + %(AdditionalLibraryDirectories) + false + + + + + + + X64 + + + Disabled + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + false + + + $(Platform)\$(Configuration)\$(ProjectName).lib + %(AdditionalLibraryDirectories) + false + + + MachineX64 + + + + + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + false + false + + + $(Platform)\$(Configuration)\$(ProjectName).lib + %(AdditionalLibraryDirectories) + false + + + + + + + X64 + + + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + false + false + + + $(Platform)\$(Configuration)\$(ProjectName).lib + %(AdditionalLibraryDirectories) + false + + + MachineX64 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {89385c74-5860-4174-9caf-a39e7c48909c} + + + {AB03E82B-48B1-4374-B32A-A1AF83DDC6C2} + false + + + {87ee9da4-de1e-4448-8324-183c98dca588} + false + + + {202d7a4e-760d-4d0e-afa1-d7459ced30ff} + false + + + + + + \ No newline at end of file diff --git a/src/mod/languages/mod_v8/mod_v8.2012.vcxproj.filters b/src/mod/languages/mod_v8/mod_v8.2012.vcxproj.filters new file mode 100644 index 0000000000..1f35875ef7 --- /dev/null +++ b/src/mod/languages/mod_v8/mod_v8.2012.vcxproj.filters @@ -0,0 +1,116 @@ + + + + + + BaseClasses + + + BaseClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + FSClasses + + + + + + BaseClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + FSClasses\include + + + + + {57f55a0b-1790-4632-95e6-19acbe9015d3} + + + {dcda9b2f-890a-4820-84e3-4bbcf8f0b62c} + + + {ed9652cf-79bd-4567-9a98-1ed5077d5805} + + + {3efc073f-f052-4e3d-921c-8d6b6a5a7215} + + + \ No newline at end of file diff --git a/src/mod/languages/mod_v8/mod_v8.cpp b/src/mod/languages/mod_v8/mod_v8.cpp new file mode 100644 index 0000000000..2f37a8cd0e --- /dev/null +++ b/src/mod/languages/mod_v8/mod_v8.cpp @@ -0,0 +1,779 @@ +/* + * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2013-2014, Peter Olsson + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is ported from FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Peter Olsson + * Anthony Minessale II + * William King + * + * mod_v8.cpp -- JavaScript FreeSWITCH module + * + */ + +/* + * This module executes JavaScript using Google's V8 JavaScript engine. + * + * It extends the available JavaScript classes with the following FS related classes; + * CoreDB Adds features to access the core DB (SQLite) in FreeSWITCH. (on request only) + * CURL Adds some extra methods for CURL access. (on request only) + * DTMF Object that holds information about a DTMF event. + * Event Object that holds information about a FreeSWITCH event. + * File Class to reflect the Spidermonkey built-in class "File". Not yet implemented! (on request only) + * FileIO Simple class for basic file IO. + * ODBC Adds features to access any ODBC available database in the system. (on request only) + * PCRE Adds features to do regexp using the PCRE implementeation. + * Request Class for extra features during API call from FS (using 'jsapi' function). This class cannot be constructed from JS code! + * The Request class is only availble when started from 'jsapi' FS command, and only inside the predefined variable 'request'. + * Session Main FS class, includes all functions to handle a session. + * Socket Class for communicating over a TCP/IP socket. (on request only) + * TeleTone Class used to play tones to a FS channel. (on request only) + * XML XML parsing class, using the features from switch_xml. (on request only) + * + * Some of the classes above are available on request only, using the command [use('Class');] before using the class for the first time. + * + * It also adds quite a few global functions, directly available for the user (see fsglobal.cpp for the implementation). + * + * Depedning on how the script was started from FreeSWITCH, some variables might be defined already; + * session If the script is started from the dialplan, the variable 'session' holds the session for the current call. + * request If the script is started using 'jsapi' function, the variable 'request' is an instance of the Request class. + * message If the script is started as a chat application, the actual FreeSWITCH event will be available in the variable 'message'. + * + * All classes are implemented in a pair of hpp/cpp files, named after the class. For instance; class "File" is implemented in fsfile.cpp. + * + */ + +#include "mod_v8.h" +#include + +#ifdef V8_ENABLE_DEBUGGING +#include +#endif + +/* Global JavaScript functions */ +#include "fsglobal.hpp" + +/* Common JavaScript classes */ +#include "fsrequest.hpp" /* Only loaded during 'jsapi' call */ +#include "fspcre.hpp" +#include "fsevent.hpp" +#include "fssession.hpp" +#include "fsdtmf.hpp" +#include "fsfileio.hpp" + +/* Optional JavaScript classes (loaded on demand) */ +#include "fscoredb.hpp" +#include "fscurl.hpp" +#include "fsteletone.hpp" +#include "fssocket.hpp" +#include "fsodbc.hpp" +#include "fsxml.hpp" +#include "fsfile.hpp" + +using namespace std; +using namespace v8; + +SWITCH_BEGIN_EXTERN_C + +/* FreeSWITCH module load definitions */ +SWITCH_MODULE_LOAD_FUNCTION(mod_v8_load); +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_v8_shutdown); +SWITCH_MODULE_DEFINITION_EX(mod_v8, mod_v8_load, mod_v8_shutdown, NULL, SMODF_GLOBAL_SYMBOLS); + +/* API interfaces */ +static switch_api_interface_t *jsrun_interface = NULL; +static switch_api_interface_t *jsapi_interface = NULL; + +/* Module manager for loadable modules */ +module_manager_t module_manager = { 0 }; + +/* Loadable module struct */ +typedef struct { + char *filename; + void *lib; + const v8_mod_interface_t *module_interface; + v8_mod_init_t v8_mod_init; +} v8_loadable_module_t; + +#ifdef V8_ENABLE_DEBUGGING +static bool debug_enable_callback = false; +static int debug_listen_port = 9999; +static bool debug_wait_for_connection = true; +static bool debug_manual_break = true; +#endif + +using namespace v8; + +static switch_status_t v8_mod_init_built_in(const v8_mod_interface_t *mod_interface) +{ + switch_assert(mod_interface); + + switch_core_hash_insert(module_manager.load_hash, (char *) mod_interface->name, mod_interface); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Successfully Loaded [%s]\n", mod_interface->name); + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t v8_mod_load_file(const char *filename) +{ + v8_loadable_module_t *module = NULL; + switch_dso_lib_t dso = NULL; + switch_status_t status = SWITCH_STATUS_SUCCESS; + switch_loadable_module_function_table_t *function_handle = NULL; + v8_mod_init_t v8_mod_init = NULL; + const v8_mod_interface_t *module_interface = NULL, *mp; + char *derr = NULL; + const char *err = NULL; + + switch_assert(filename != NULL); + + if (!(dso = switch_dso_open(filename, 1, &derr))) { + status = SWITCH_STATUS_FALSE; + } + + if (derr || status != SWITCH_STATUS_SUCCESS) { + err = derr; + goto err; + } + + function_handle = (switch_loadable_module_function_table_t *)switch_dso_data_sym(dso, "v8_mod_init", &derr); + + if (!function_handle || derr) { + status = SWITCH_STATUS_FALSE; + err = derr; + goto err; + } + + v8_mod_init = (v8_mod_init_t) (intptr_t) function_handle; + + if (v8_mod_init == NULL) { + err = "Cannot Load"; + goto err; + } + + if (v8_mod_init(&module_interface) != SWITCH_STATUS_SUCCESS) { + err = "Module load routine returned an error"; + goto err; + } + + if (!(module = (v8_loadable_module_t *) switch_core_permanent_alloc(sizeof(*module)))) { + err = "Could not allocate memory\n"; + } + + err: + + if (err || !module) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Loading module %s\n**%s**\n", filename, switch_str_nil(err)); + switch_safe_free(derr); + return SWITCH_STATUS_GENERR; + } + + module->filename = switch_core_permanent_strdup(filename); + module->v8_mod_init = v8_mod_init; + module->module_interface = module_interface; + + module->lib = dso; + + if ((mp = module->module_interface)) { + switch_core_hash_insert(module_manager.load_hash, (char *) mp->name, (void *) mp); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Successfully Loaded [%s]\n", module->filename); + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t v8_load_module(const char *dir, const char *fname) +{ + switch_size_t len = 0; + char *path; + char *file; + +#ifdef WIN32 + const char *ext = ".dll"; +#else + const char *ext = ".so"; +#endif + + if ((file = switch_core_strdup(module_manager.pool, fname)) == 0) { + return SWITCH_STATUS_FALSE; + } + + if (*file == '/') { + path = switch_core_strdup(module_manager.pool, file); + } else { + if (strchr(file, '.')) { + len = strlen(dir); + len += strlen(file); + len += 4; + path = (char *) switch_core_alloc(module_manager.pool, len); + switch_snprintf(path, len, "%s%s%s", dir, SWITCH_PATH_SEPARATOR, file); + } else { + len = strlen(dir); + len += strlen(file); + len += 8; + path = (char *) switch_core_alloc(module_manager.pool, len); + switch_snprintf(path, len, "%s%s%s%s", dir, SWITCH_PATH_SEPARATOR, file, ext); + } + } + + return v8_mod_load_file(path); +} + +SWITCH_END_EXTERN_C + +static switch_status_t load_modules(void) +{ + const char *cf = "v8.conf"; + switch_xml_t cfg, xml; + unsigned int count = 0; + +#ifdef WIN32 + const char *ext = ".dll"; + const char *EXT = ".DLL"; +#elif defined (MACOSX) || defined (DARWIN) + const char *ext = ".dylib"; + const char *EXT = ".DYLIB"; +#else + const char *ext = ".so"; + const char *EXT = ".SO"; +#endif + + memset(&module_manager, 0, sizeof(module_manager)); + switch_core_new_memory_pool(&module_manager.pool); + + switch_core_hash_init(&module_manager.load_hash, module_manager.pool); + + if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) { + switch_xml_t mods, ld; + + if ((mods = switch_xml_child(cfg, "modules"))) { + for (ld = switch_xml_child(mods, "load"); ld; ld = ld->next) { + const char *val = switch_xml_attr_soft(ld, "module"); + if (!zstr(val) && strchr(val, '.') && !strstr(val, ext) && !strstr(val, EXT)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Invalid extension for %s\n", val); + continue; + } + v8_load_module(SWITCH_GLOBAL_dirs.mod_dir, val); + count++; + } + } + switch_xml_free(xml); + + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Open of %s failed\n", cf); + } + + return SWITCH_STATUS_SUCCESS; +} + +static int env_init(JSMain *js) +{ + /* Init all "global" functions first */ + const js_function_t *fs_proc = FSGlobal::GetFunctionDefinitions(); + for (int i = 0; fs_proc[i].name && fs_proc[i].func; i++) { + js->AddJSExtenderFunction(fs_proc[i].func, fs_proc[i].name); + } + + /* Init all basic classes made available from FreeSWITCH */ + js->AddJSExtenderClass(FSSession::GetClassDefinition()); + js->AddJSExtenderClass(FSFileIO::GetClassDefinition()); + js->AddJSExtenderClass(FSEvent::GetClassDefinition()); + js->AddJSExtenderClass(FSDTMF::GetClassDefinition()); + js->AddJSExtenderClass(FSPCRE::GetClassDefinition()); + /* To add a class that will always be available inside JS, add the definition here */ + + return 1; +} + +static void v8_error(Isolate* isolate, TryCatch* try_catch) +{ + HandleScope handle_scope(isolate); + String::Utf8Value exception(try_catch->Exception()); + const char *exception_string = js_safe_str(*exception); + Handle message = try_catch->Message(); + const char *msg = ""; + string filename = __FILE__; + int line = __LINE__; + string text = ""; + JSMain *js = JSMain::GetScriptInstanceFromIsolate(isolate); + + if (js && js->GetForcedTermination()) { + js->ResetForcedTermination(); + switch_log_printf(SWITCH_CHANNEL_ID_LOG, js->GetForcedTerminationScriptFile(), modname, js->GetForcedTerminationLineNumber(), NULL, SWITCH_LOG_NOTICE, "Script exited with info [%s]\n", js->GetForcedTerminationMessage()); + return; + } + + if (!message.IsEmpty()) { + String::Utf8Value fname(message->GetScriptResourceName()); + + if (*fname) { + filename = *fname; + } + + line = message->GetLineNumber(); + msg = exception_string; + + String::Utf8Value sourceline(message->GetSourceLine()); + if (*sourceline) { + text = *sourceline; + } + } else { + msg = exception_string; + } + + if (!msg) { + msg = ""; + } + + if (text.length() > 0) { + switch_log_printf(SWITCH_CHANNEL_ID_LOG, filename.c_str(), modname, line, NULL, SWITCH_LOG_ERROR, "Exception: %s (near: \"%s\")\n", msg, text.c_str()); + } else { + switch_log_printf(SWITCH_CHANNEL_ID_LOG, filename.c_str(), modname, line, NULL, SWITCH_LOG_ERROR, "Exception: %s\n", msg); + } +} + +static char *v8_get_script_path(const char *script_file) +{ + const char *p; + char *ret = NULL; + const char delims[] = "/\\"; + const char *i; + + if (script_file) { + for (i = delims; *i; i++) { + if ((p = strrchr(script_file, *i))) { + js_strdup(ret, script_file); + *(ret + (p - script_file)) = '\0'; + return ret; + } + } + js_strdup(ret, "."); + return ret; + } else { + return NULL; + } +} + +static int v8_parse_and_execute(switch_core_session_t *session, const char *input_code, switch_stream_handle_t *api_stream, switch_event_t *message) +{ + string res; + JSMain *js; + Isolate *isolate; + char *arg, *argv[512]; + int argc = 0, x = 0, y = 0; + unsigned int flags = 0; + char *path = NULL; + string result_string; + int result = 0; + + if (zstr(input_code)) { + return -1; + } + + js = new JSMain(); + isolate = js->GetIsolate(); + + env_init(js); + + /* Try to read lock the session first */ + if (session) { + if (switch_core_session_read_lock_hangup(session) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Read Lock Failure.\n"); + session = NULL; + } + } + + /* Execute the actual script */ + //isolate->Enter(); + { + Locker lock(isolate); + Isolate::Scope iscope(isolate); + { + const char *script; + + // Create a stack-allocated handle scope. + HandleScope scope(isolate); + + // Store our object internally + isolate->SetData(0, js); + + // New global template + Handle global = ObjectTemplate::New(); + + if (global.IsEmpty()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create JS global object template\n"); + } else { + /* Function to print current V8 version */ + global->Set(String::NewFromUtf8(isolate, "version"), FunctionTemplate::New(isolate, JSMain::Version)); + + /* Add all global functions */ + for (size_t i = 0; i < js->GetExtenderFunctions().size(); i++) { + js_function_t *proc = js->GetExtenderFunctions()[i]; + global->Set(String::NewFromUtf8(isolate, proc->name), FunctionTemplate::New(isolate, proc->func)); + } + + // Create a new context. + Local context = Context::New(isolate, NULL, global); + + if (context.IsEmpty()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create JS context\n"); + } else { + // Enter the created context for compiling and running the script. + Context::Scope context_scope(context); + +#ifdef V8_ENABLE_DEBUGGING + Persistent *debug_context = new Persistent(); + isolate->SetData(1, debug_context); + debug_context->Reset(isolate, context); + + //v8::Locker lck(isolate); + + if (debug_enable_callback) { + Debug::SetDebugMessageDispatchHandler(V8DispatchDebugMessages, true); + } + + if (debug_listen_port > 0) { + char *name = switch_mprintf("mod_v8-%d", (int)switch_thread_self()); + Debug::EnableAgent(name, debug_listen_port, debug_wait_for_connection); + switch_safe_free(name); + } +#endif + + /* Register all plugin classes */ + for (size_t i = 0; i < js->GetExtenderClasses().size(); i++) { + JSBase::Register(isolate, js->GetExtenderClasses()[i]); + } + + /* Register all instances of specific plugin classes */ + for (size_t i = 0; i < js->GetExtenderInstances().size(); i++) { + registered_instance_t *inst = js->GetExtenderInstances()[i]; + inst->obj->RegisterInstance(isolate, inst->name, inst->auto_destroy); + } + + /* Emaculent conception of session object into the script if one is available */ + if (session) { + FSSession *obj = new FSSession(js); + obj->Init(session, flags); + obj->RegisterInstance(isolate, "session", true); + } else { + /* Add a session object as a boolean instead, just to make it safe to check if it exists as expected */ + context->Global()->Set(String::NewFromUtf8(isolate, "session"), Boolean::New(isolate, false)); + } + + if (message) { + FSEvent::New(message, "message", js); + } + + if (api_stream) { + /* The JS class "Request" is only needed when a api_stream object is passed here */ + JSBase::Register(isolate, FSRequest::GetClassDefinition()); + + FSRequest *ptr = new FSRequest(js); + ptr->Init(input_code, api_stream); + ptr->RegisterInstance(isolate, "request", true); + } + + script = input_code; + + if (*script != '~') { + if ((arg = (char *)strchr(script, ' '))) { + *arg++ = '\0'; + argc = switch_separate_string(arg, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); + } + + // Add arguments before running script. + Local arguments = Array::New(isolate, argc); + for (y = 0; y < argc; y++) { + arguments->Set(Integer::New(isolate, y), String::NewFromUtf8(isolate, argv[y])); + } + context->Global()->Set(String::NewFromUtf8(isolate, "argv"), arguments); + context->Global()->Set(String::NewFromUtf8(isolate, "argc"), Integer::New(isolate, argc)); + } + + const char *script_data = NULL; + const char *script_file = NULL; + string s; + + if (*script == '~') { + script_data = script + 1; + script_file = "inline"; + } else { + const char *script_name = NULL; + + if (switch_is_file_path(script)) { + script_name = script; + } else if ((path = switch_mprintf("%s%s%s", SWITCH_GLOBAL_dirs.script_dir, SWITCH_PATH_SEPARATOR, script))) { + script_name = path; + } + + if (script_name) { + if (JSMain::FileExists(script_name)) { + s = JSMain::LoadFileToString(script_name); + script_data = s.c_str(); + script_file = script_name; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot Open File: %s\n", script_name); + } + } + } + + if (!script_data) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No script to execute!\n"); + } else { + /* Store our base directoy in variable 'scriptPath' */ + char *path = v8_get_script_path(script_file); + if (path) { + context->Global()->Set(String::NewFromUtf8(isolate, "scriptPath"), String::NewFromUtf8(isolate, path)); + free(path); + } + // Create a string containing the JavaScript source code. + Handle source = String::NewFromUtf8(isolate, script_data); + + TryCatch try_catch; + + // Compile the source code. + Handle