Oznámení

Sbalit
Aktuálně žádná oznámení.

NodeAmiga – JavaScript pro Amigu

Sbalit
X
 
  • Filtr
  • Čas
  • Zobrazit
Vymazat vše
new posts

    NodeAmiga – JavaScript pro Amigu

    Paweł "juen" Nowak se zaroven s prohlizecem pustil do dalsiho projektu – JavaScript runtime interpretru pro AmigaOS ve stylu Node.js
    Nejde o zadny port, vse je napsano v od zacatku v C s ohledem na klasickou Amigu.

    NodeAmiga podporuje nejpoužívanější podmnožinu funkci moderního JavaScriptu
    • Full ES5 (everything from the original spec)
    • Arrow functions, classes with extends and super, template literals, destructuring, spread/rest, default parameters
    • let/const, for-of, labeled loops
    • async/await and generators (function*, yield)
    • Optional chaining ?., nullish coalescing ??
    • Regex literals /pattern/flags
    • BigInt, Symbol, Map/Set/WeakMap/WeakSet
    • Typed arrays (Uint8Array, Int16Array, etc.), ArrayBuffer, DataView
    • Modules (both require()/CommonJS and import/ESM)
    ​Doplneny jsou i moduly pro praci s graphics a intuition knihovnami, takze lze vytvaret i GUI programy

    NodeAmiga
    Dokumentace

    Kratky priklad
    Code:
    /*
    * gui_translator.js - Google Translate with GadTools GUI
    *
    * Two text fields, two language selectors, two translate buttons.
    * Type text in either field and translate in either direction.
    *
    * Run: NodeAmiga examples/gui_translator.js
    * Requires: bsdsocket.library + AmiSSL (for HTTPS)
    */
    
    var gui = require('gui');
    var http = require('http');
    
    var fromNames = ['Auto', 'English', 'Polski', 'Deutsch', 'Francais', 'Espanol', 'Italiano', 'Portugues', 'Russian', 'Czech', 'Dutch', 'Swedish', 'Ukrainian'];
    var fromCodes = ['auto', 'en', 'pl', 'de', 'fr', 'es', 'it', 'pt', 'ru', 'cs', 'nl', 'sv', 'uk'];
    
    var toNames = ['English', 'Polski', 'Deutsch', 'Francais', 'Espanol', 'Italiano', 'Portugues', 'Russian', 'Czech', 'Dutch', 'Swedish', 'Ukrainian'];
    var toCodes = ['en', 'pl', 'de', 'fr', 'es', 'it', 'pt', 'ru', 'cs', 'nl', 'sv', 'uk'];
    
    /* --- Create window with gadgets --- */
    
    var win = gui.createWindow({
        title: 'Translator',
        width: 440,
        height: 120,
        left: 40,
        top: 30,
        gadgets: [
             { kind: 'cycle', id: 1, label: 'From:',
               items: fromNames, left: 60, top: 5, width: 180, height: 14 },
    
             { kind: 'string', id: 2,
               left: 10, top: 25, width: 420, height: 14, value: '' },
    
             { kind: 'button', id: 3, label: 'Translate v',
               left: 60, top: 48, width: 140, height: 14 },
    
             { kind: 'button', id: 4, label: '^ Translate',
               left: 210, top: 48, width: 140, height: 14 },
    
             { kind: 'cycle', id: 5, label: 'To:',
               items: toNames, left: 60, top: 72, width: 180, height: 14 },
    
             { kind: 'string', id: 6,
               left: 10, top: 92, width: 420, height: 14, value: '' }
        ]
    });
    
    /* --- Translation function --- */
    
    function translate(text, from, to) {
        var url = 'https://translate.googleapis.com/translate_a/single?client=gtx'
                   + '&sl=' + encodeURIComponent(from)
                   + '&tl=' + encodeURIComponent(to)
                   + '&dt=t&q=' + encodeURIComponent(text);
    
        try {
            var res = http.get(url);
            if (res && res.body) {
                var data = JSON.parse(res.body);
                if (data && data[0] && data[0][0] && data[0][0][0]) {
                    return data[0][0][0];
                }
            }
            return 'Error: no response from server';
        } catch (e) {
            return 'Error: ' + e.message;
        }
    }
    
    /* --- Main event loop --- */
    
    console.log('Translator ready. Close window to exit.');
    
    while (true) {
        var ev = gui.waitEvent(win);
        if (!ev || ev.type === 'close') break;
    
        if (ev.type === 'gadgetup') {
            if (ev.id === 3) {
                /* Translate top -> bottom */
                var text = gui.get(win, 2);
                if (text && text.length > 0) {
                    var sl = fromCodes[gui.get(win, 1)];
                    var tl = toCodes[gui.get(win, 5)];
                    gui.setTitle(win, 'Translating...');
                    var result = translate(text, sl, tl);
                    gui.set(win, 6, result);
                    gui.setTitle(win, 'Translator');
                }
            } else if (ev.id === 4) {
                /* Translate bottom -> top */
                var text = gui.get(win, 6);
                if (text && text.length > 0) {
                    var tl = fromCodes[gui.get(win, 1)];
                    var sl = toCodes[gui.get(win, 5)];
                    if (tl === 'auto') tl = 'en';
                    gui.setTitle(win, 'Translating...');
                    var result = translate(text, sl, tl);
                    gui.set(win, 2, result);
                    gui.setTitle(win, 'Translator');
                }
            }
        }
    }
    
    gui.closeWindow(win);
    console.log('Bye!');​
    Klikni pro plné zobrazení obrázku  Jméno: image.png Počet zobrazení: 0 Velikost: 5,9 KB ID: 178045
    Naposledy upravil ExiE; 03.05.2026, 01:41:47.

    #2
    Jen pro zajimavost, v ramci syntaxe jsem nevidel nic, co by mi rikalo, "tohle je novejsi javascript nez ten co znam" a to je ES3, zde je uvadena podpora ES5. Je to jen priklad, ale ze zajimavosti jsem se AI zeptal, jestli je tam neco co nebezi v ES3 a diskutabilni je tam pouziti JSON.parse, ostatni vse cisty i starsí javascript, alepson ES3 kterou znam trochu ja. Ale upravil jsem si mou domnenku, ze ES5-ka ma k dispozici i dalsi deklaraci promennych nez je VAR, ale to je az od ES6-ky. OK. Ale VAR je velice dobre pouzitelny, mam rad jednoduchost v programovani. Nevyhoda VAR o ktere vim, je ze pokud mate cyklus a zpracovavate navratovou hodnotu z nejake funkce, tak diky te navratove hodnote budete mit kazda iterace jen posledni a tedy nejvyssi hodnotu. Musi se pak pouzivat IIFE metoda, ktera zabezpeci, ze navratove hodnote bude prirazena spravna promenna z cyklu a ne jen ta nejvyssi. Tohle chovani resi az promenna LET, kde se jiz IIFE metoda nemusi pouzit, ale to je az javascript ES6
    Amiga - PMD 85

    Komentovat

    Zpracovávám...
    X