Skip to content
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

slicing support #19

Open
husio-org opened this issue Jul 1, 2013 · 4 comments
Open

slicing support #19

husio-org opened this issue Jul 1, 2013 · 4 comments

Comments

@husio-org
Copy link

Hi!

I need slicing for a library that i am porting.

I have started to look into how to make it work. The main issue seems to me that Buffer.slice and Array.slice work in a different way. The first references to the original buffer, and modifications alter the original buffer, while the second generates a new copy.

Did you started to look into this? comments / directions are welcome!

@toots
Copy link
Owner

toots commented Jul 1, 2013

Hi,

I haven't started on this yet so feel free to submit a PR for it.

I don't think that it'll be possible to have the sliced buffer share its elements with the original. Hopefully, this is a feature that is not widely used...

@husio-org
Copy link
Author

Yes, I am working on it, because I need it with something I am porting. Will send PR when I have something.

The particular use case is splitting a received buffer into several sub packets. It seems to be read-only slices.

I will try to comply with the original functionality, but I also think its going to be hard. I am currently looking into getters/setters and pure prototypal inheritance...

If it is not possible, we could log a warning and encourage people to migrate buffer[]= to buffer.set() if they want to write to slices updating the original buffer.

@danthegoodman
Copy link

I needed this fixed too, so to minimize my changes I simply copied the data from one buffer to another, like so:

for (var i = 0; i < this.length; i++) {
  this[i] = subject.get(i+offset);
}

This was sufficient to get the project I'm working on up and running.

I have a fork up, but I have not submitted a PR. Since the buffer was being copied during normal creation, I figured this was a good approach, but I'd still like to hear your thoughts.

@husio-org
Copy link
Author

I have been looking at how to do this as clean as possible...

The option would be to complete the implementation of get and set, using the parent and traslating the index with offset.

Then using Object.defineProperty on the index properties: 0,1,2... to use delegate on get/set.

That would get this working for modern browsers only, coz IE8 does not support Object.defineProperty unless on a DOM object.

Whatever the case, something is going to break.

I would say that the use case of updating a slice and specting the parent to get updated also is much less frequent than old browsers.

I would go with dan suggestion... I am going to test it in our particular case...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants