murarth / ketos

Lisp dialect scripting and extension language for Rust programs
Apache License 2.0
751 stars 45 forks source link

Allow Global Scope clone #22

Closed samdesota closed 8 years ago

samdesota commented 8 years ago

It is possible to allow cloning of the global scope?

I have a server that receives many requests, I want to be able to execute code from a base scope from each request, without values or macros defined in any given request leaking into the global scope.

murarth commented 8 years ago

I don't think implementing Clone for GlobalScope is a great idea. It could be confused with cloning the Rc handle and it may not be clear that cloning the scope itself still shares some data (some internal GlobalScope fields are themselves Rc).

If you want to share the Rc fields, you can use GlobalScope::new_using to create a new Scope. Otherwise, you might want to create a new global scope with new data fields.

samdesota commented 8 years ago

As far as I have tested, new_using doesn't actually copy the scope with the values injected into it, it just copies the IO and the module loader.

The only way I can inject macros, etc. into a scope right now, is to re-compile the macros from source every time I want to run a script.

What I'm asking is there any way to take a scope with everything it contains, and cleanly create a new scope with those values + macros so that I can run code in it without effecting the original scope.

It doesn't need to be the actual clone trait. I guess I'm just confused as to why this is so hard, I spent a couple hours trying solve this the yesterday

samdesota commented 8 years ago

From what I can see in the source, I need to be able to clone the actual Namespace

murarth commented 8 years ago

Oh, I didn't realize that you did want values in the base scope to be cloned. A clone-like method does seem to be the clearest way of allowing this.

I've just pushed a commit adding the method GlobalScope::clone_scope. I'll publish to Cargo (under version 0.3.1) once it passes CI.

murarth commented 8 years ago

Published.

samdesota commented 8 years ago

Awesome! Great work on the project.