想在資源受限的嵌入式設備上,只保留解釋器的執行部分,把其他部分分離出去。
怎麼樣分析一個解釋器,把它的執行部分整理出來??
具體針對Javascript解釋器,Espruino
題主問的JavaScript引擎是Espruino,這個解釋器已經非常非常非常小了,不知道題主還需要剝離哪些東西出去?這個JavaScript引擎的執行部分是一個解釋器,而且是邊parse邊解釋的那種,連AST都不構造。這種實現方式讓我想起了最早期的PHP…這是實現解釋器最慢的做法“之一”了…所以它的解釋器主體就在這個文件裡:Espruino/jsparse.c at master · espruino/Espruino · GitHub它依賴jslex.[h|c]與jsvar.[h|c]。jslex負責詞法分析,jsvar負責內存管理(包括對象結構體的定義以及一個超簡易的mark-sweep GC等)。src目錄裡剩下的文件,jswrap開頭的都是用於實現JavaScript標准庫的,這些如果有用不到的可以刪掉;然後剩下的js開頭的文件(例如jsdevice.[h|c])主要都是跟操作嵌入式設備相關的,這些也可以看題主的需求裁剪。話說有個同樣是不構造AST直接邊parse邊解釋執行的JavaScript實現,tiny-js,也非常小。官網:Pur3.co.uk :: TinyJS而它其實就是Espruino的JavaScript實現的前身。難怪會用同樣的思路去實現…呃Espruino - PressAs a Linux user Gordon was aware that whatever tools were used had to work reliably on multiple platforms without needing extra software, so building an interpreter into the microcontroller seemed the obvious way forwards. BASIC interpreters for microcontrollers have been around for a while (like the amazing Maximite), but until recently the amount of built-in RAM and Flash in microcontrollers has been a stumbling block for more complex languages. When Gordon saw the STM32F1 development boards with 128kb of Flash and 8kb of RAM he realised he could use the lessons learned from his TinyJS JavaScript interpreter (originally built for Morphyre) to produce something that would run within those limits - and so Espruino started!====================================題主問的Python解釋器MicroPython看起來比Espruino大一些。它至少包括了很完整的編譯器+字節碼解釋器+GC,這核心可裁剪的東西不太多了。能動的主要是外圍包含的庫了…