Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Possible overlooked mistakes in prelude/types.js? #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
CL-Jeremy opened this issue Sep 2, 2024 · 0 comments
Open

Possible overlooked mistakes in prelude/types.js? #1341

CL-Jeremy opened this issue Sep 2, 2024 · 0 comments

Comments

@CL-Jeremy
Copy link

CL-Jeremy commented Sep 2, 2024

I was looking into possibilities to refactor prelude and stumbled on #1057. Does the change in this specific file even work like this, since typ is a var in a function and not a global?

// Creates constructor functions for array pointer types. Returns a new function
// instance each time to make sure each type is independent of the other.
var $arrayPtrCtor = () => {
return function (array) {
this.$get = () => { return array; };
this.$set = function (v) { typ.copy(this, v); };
this.$val = array;
};
}

Wouldn't it be necessary to change line 64 to return function typ(array) { instead?

Related to the above code: there are also 2 references to the prototype of the class, which is empty unless the methods are defined as methods:

case $kindStruct:
typ = function (v) { this.$val = v; };
typ.wrapped = true;
typ.ptr = $newType(4, $kindPtr, "*" + string, false, pkg, exported, constructor);
typ.ptr.elem = typ;
typ.ptr.prototype.$get = function () { return this; };
typ.ptr.prototype.$set = function (v) { typ.copy(this, v); };

With the code kept as-is, the lines 242-243 shouldn't be able to override these methods, as the function assignments as in $arrayPtrCtor() would land in the class constructor and happen only at instantiation.

Here is my suggestion for this specific part. I was trying to give the file some type annotations to increase readability. If this change holds true, there might be other JavaScript snippets that are worth looking at regarding similar issues.

/**
 * Creates constructor functions for array pointer types. Returns a new function
 * instace each time to make sure each type is independent of the other.
 * 
 * @template T
 * @typedef {{
 *  $get(): T,
 *  $set(v: T): void,
 *  $val: T 
 * }} Typ<T>
 * @typedef {(new <T>(v: T) Typ<T>) & {
 *  prototype: Omit<Typ<T>, "$val">,
 *  wrapped: boolean,
 *  keyFor(v: T): T,
 *  elem?: TypClass<T>,
 *  len?: number,
 *  comparable?: boolean,
 *  ptr?: TypClass<T>,
 *  init?(...args): void,
 *  nil?: Typ<T>
 *  copy?(v1: T, v2: T): void,
 * }} TypClass<T>
 * @type {() => TypClass<T>}
 */
var $arrayPtrCtor = () => {
    return class typ {
        $get() { return this.$val; }
        $set(v) { typ.copy(this, v); }
        constructor(array) { this.$val = array; }
    };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants