jscl icon indicating copy to clipboard operation
jscl copied to clipboard

how to create javascript object? (i.e Float32Array)

Open byulparan opened this issue 8 years ago • 5 comments

I can't find way...sorry for simple question.

new Float32Array(10)

cl::new seems just create empty object.

byulparan avatar Apr 21 '16 18:04 byulparan

I think it is a perfect question, I am not sure but it doesn't seem to be possible yet. So it is a good opportunity to implement it.

My proposal would be a special form new (renaming the existing new to something like make-object), and use it like (new <fn> <arg> ...), e.g: (new #j:Date 0).

new can't be an ordinary function

It doesn't compose expressions nicely. For instance, you can't use a function returning your constructor after new. Given:

function f () { this.x = 10; }
function g () { return f; }

this is not correct new (g()), but var fn = g(); new fn() would be.

davazp avatar Apr 22 '16 06:04 davazp

Why isn't new (g()) correct? Seems working fine at least in chrome and firefox. You can even pass parameters to f with new (g())(x).

Andrea

On Fri, Apr 22, 2016 at 8:40 AM, David Vázquez Púa <[email protected]

wrote:

I think it is a perfect question, I am not sure but it doesn't seem to be possible yet. So it is a good opportunity to implement it.

My proposal would be a special form new (renaming the existing new to something like make-object), and use it like (new ...), e.g: (new #j:Date 0).

new can't be an ordinary function

It doesn't compose expressions nicely. For instance, you can't use a function returning your constructor after new. Given:

function f () { this.x = 10; }function g () { return f; }

this is not correct new (g()), but var fn = g(); new fn() would be.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/davazp/jscl/issues/228#issuecomment-213288700

6502 avatar Apr 22 '16 07:04 6502

You are right. I don't know what I was thinking. It works :). My concern was that

(new (f X)) didn't seem a good syntax.

It would still need new to be a special operator to compile the function call differently.

davazp avatar Apr 22 '16 07:04 davazp

Anyway, I think that

(new #'f x)

will work. It allow us to easily use any expression for the constructor and pass arguments. It can be implemented as a built-in function.

We still need to decide how it will interact with this in Lisp, in case we want to implement a constructor function in Lisp.

davazp avatar Apr 22 '16 11:04 davazp

This branch https://github.com/davazp/jscl/compare/ffi-improvements contains code to be able to use cl::this from Lisp. It binds the variable in the prelude of every function, outside of any function introduced by the compiler.

I think that could work.

davazp avatar Apr 22 '16 11:04 davazp