bayrell / BayLang

BayLang compiler source code
https://bayrell.org/ru/docs/baylang
Apache License 2.0
4 stars 0 forks source link

Runtime Library #163

Open ildar-ceo opened 2 months ago

ildar-ceo commented 2 months ago

Полифилы для JS

if (typeof Runtime == 'undefined') Runtime = {};
Runtime.rtl = function(){};
Object.assign(Runtime.rtl,
{
    _classes: {},
    use(class_name){ return this._classes[class_name]; },
    exists(value){ return value != null && value != undefined },
    class_exists(class_name){ this.exists(use(class_name)); },
    method_exists(obj, method_name)
    {
        if (typeof obj == "string") obj = Runtime.rtl.use(obj);
        var f = obj[method_name];
        if (!this.exists(f)) return false;
        if (typeof f != 'function') return false;
        return true;
    },
    callback(object, method_name)
    {
        if (!this.method_exists(object, method_name))
            throw new Runtime.Exception.ItemNotFound(method_name, "Method");
        var f = function()
        {
            return object[method_name].bind(obj).apply(null, arguments);
        };
        f.object = object;
        f.method_name = method_name;
        return f;
    },
    newInstance(class_name, args)
    {
        var f = this.use(class_name);
        if (!this.exists(f)) throw new Runtime.Exception.ItemNotFound(class_name, "Class");
        var create = Function.prototype.bind.apply(f, args ? args.toNative() : []);
        return new create;
    },
    is_implements(obj, interface_name)
    {
        if (typeof obj.getClassName == undefined) return false;
        return this.class_implements(obj.getClassName(), interface_name);
    },
    class_implements(class_name, interface_name)
    {
        var class_obj = use(class_name);
        var interface_obj = use(interface_name);
        if (!this.exists(class_obj)) return false;
        if (!this.exists(interface_obj)) return false;
        while (class_obj != null)
        {
            if (class_obj.__implements__ && class_obj.__implements__.indexOf(interface_obj) > -1)
                return true;
            class_obj = class_obj.__proto__;
        }
        return false;
    },

    /* Context */
    _global_context: null,
    getContext(){ return this._global_context; },
    setContext(ctx){ this._global_context = ctx; },
});
Runtime.rtl._classes["Runtime.rtl"] = Runtime.rtl;