this repo has no description
at main 178 lines 4.3 kB view raw
1// MIT License 2 3// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com 4 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to deal 7// in the Software without restriction, including without limitation the rights 8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9// copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11 12// The above copyright notice and this permission notice shall be included in all 13// copies or substantial portions of the Software. 14 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21// SOFTWARE. 22 23#include "script.h" 24#include "tools.h" 25 26#include <stdio.h> 27 28#define MAX_SUPPORTED_LANGS (16) 29 30#if defined(TIC_RUNTIME_STATIC) 31 32#if defined (TIC_BUILD_WITH_LUA) 33extern tic_script EXPORT_SCRIPT(Lua); 34#endif 35 36#if defined(TIC_BUILD_WITH_RUBY) 37extern tic_script EXPORT_SCRIPT(Ruby); 38#endif 39 40#if defined(TIC_BUILD_WITH_JS) 41extern tic_script EXPORT_SCRIPT(Js); 42#endif 43 44#if defined(TIC_BUILD_WITH_MOON) 45extern tic_script EXPORT_SCRIPT(Moon); 46#endif 47 48#if defined(TIC_BUILD_WITH_FENNEL) 49extern tic_script EXPORT_SCRIPT(Fennel); 50#endif 51 52#if defined(TIC_BUILD_WITH_SQUIRREL) 53extern tic_script EXPORT_SCRIPT(Squirrel); 54#endif 55 56#if defined(TIC_BUILD_WITH_SCHEME) 57extern tic_script EXPORT_SCRIPT(Scheme); 58#endif 59 60#if defined(TIC_BUILD_WITH_WREN) 61extern tic_script EXPORT_SCRIPT(Wren); 62#endif 63 64#if defined(TIC_BUILD_WITH_WASM) 65extern tic_script EXPORT_SCRIPT(Wasm); 66#endif 67 68#if defined(TIC_BUILD_WITH_JANET) 69extern tic_script EXPORT_SCRIPT(Janet); 70#endif 71 72#if defined(TIC_BUILD_WITH_PYTHON) 73extern tic_script EXPORT_SCRIPT(Python); 74#endif 75 76#endif 77 78static const tic_script *Scripts[MAX_SUPPORTED_LANGS + 1] = 79{ 80#if defined(TIC_RUNTIME_STATIC) 81 #if defined (TIC_BUILD_WITH_LUA) 82 &EXPORT_SCRIPT(Lua), 83 #endif 84 85 #if defined(TIC_BUILD_WITH_RUBY) 86 &EXPORT_SCRIPT(Ruby), 87 #endif 88 89 #if defined(TIC_BUILD_WITH_JS) 90 &EXPORT_SCRIPT(Js), 91 #endif 92 93 #if defined(TIC_BUILD_WITH_MOON) 94 &EXPORT_SCRIPT(Moon), 95 #endif 96 97 #if defined(TIC_BUILD_WITH_FENNEL) 98 &EXPORT_SCRIPT(Fennel), 99 #endif 100 101 #if defined(TIC_BUILD_WITH_SCHEME) 102 &EXPORT_SCRIPT(Scheme), 103 #endif 104 105 #if defined(TIC_BUILD_WITH_SQUIRREL) 106 &EXPORT_SCRIPT(Squirrel), 107 #endif 108 109 #if defined(TIC_BUILD_WITH_WREN) 110 &EXPORT_SCRIPT(Wren), 111 #endif 112 113 #if defined(TIC_BUILD_WITH_WASM) 114 &EXPORT_SCRIPT(Wasm), 115 #endif 116 117 #if defined(TIC_BUILD_WITH_JANET) 118 &EXPORT_SCRIPT(Janet), 119 #endif 120 121 #if defined(TIC_BUILD_WITH_PYTHON) 122 &EXPORT_SCRIPT(Python), 123 #endif 124 125#endif 126 127 NULL, 128}; 129 130static s32 compareScripts(const void* a, const void* b) 131{ 132 const tic_script* script1 = *(const tic_script**)a; 133 const tic_script* script2 = *(const tic_script**)b; 134 135 if (script1->id < script2->id) return -1; 136 if (script1->id > script2->id) return 1; 137 return 0; 138} 139 140void tic_add_script(const tic_script* script) 141{ 142 s32 index = 0; 143 FOREACH_LANG(it) 144 { 145 if(it->id == script->id || strcmp(it->name, script->name) == 0) 146 { 147 // script already exists 148 return; 149 } 150 151 index++; 152 } 153 154 if(index < MAX_SUPPORTED_LANGS) 155 { 156 Scripts[index] = script; 157 qsort(Scripts, index + 1, sizeof Scripts[0], (int (*)(const void *, const void *))compareScripts); 158 } 159} 160 161const tic_script** tic_scripts() 162{ 163 return Scripts; 164} 165 166const tic_script* tic_get_script(tic_mem* memory) 167{ 168 FOREACH_LANG(script) 169 { 170 if(script->id == memory->cart.lang 171 || strcmp(tic_tool_metatag(memory->cart.code.data, "script", script->singleComment), script->name) == 0) 172 return script; 173 } 174 175 static const tic_script empty; 176 177 return *Scripts ? *Scripts : &empty; 178}