These are chat archives for ChaiScript/ChaiScript

4th
Apr 2015
Victor Lopez
@VictorJL
Apr 04 2015 15:49
Hi
I've managed this code to run flawlessly but there are some questions regarding templates and polymorphism inside ChaiScript. https://github.com/VictorJL/libcrosswind/blob/master/tests/sonic_the_hedgehog/main.cpp
For example, engine->stage->create_scene<game::scenes::green_hill_zone>(); would take a template parameter and let's suppose green_hill_zone derives from composition::scene, there fore when we call create_scene we'll get a shared_pointer to a composition_scene object. How can we implement that in ChaiScript?
Victor Lopez
@VictorJL
Apr 04 2015 15:56
Let's say we add the base class to the ChaiScript environment and then we inherit inside the .chai code.
Victor Lopez
@VictorJL
Apr 04 2015 16:01
@lefticus Actually, mingw supports c++1y, the code being written in libcrosswind is C++14, TDM-GCC is being used though.
Victor Lopez
@VictorJL
Apr 04 2015 16:12
ChaiScript takes a bit of time to compile and under debug mode using CLion + TDM-GCC it throws in dispatchkit.hpp 478:13 chaiscript::exception::name_conflict_error(name), "eval_error_type".
Sorry if I had any typo in the previous messages (therefore, composition::scene), I'm writing this in a rush for development :smile:, also, I'm unaware of memory management for ChaiScript objects, what's their life time when doing chai.eval? Internally, how are they managed?
Andreas Reischuck
@arBmind
Apr 04 2015 16:22
@VictorJL chaischript uses shared_ptr internally. As long has you have a reference to the pointer around, it's kept alive
CS has not notion of templates... but you can register a function that creates your instance
CS stores all values as Boxed_Value which contains a shared_ptr to the value. Storing shared_ptr works perfectly but creates another level of nesting - shared_ptr<shared_ptr<T>>
Andreas Reischuck
@arBmind
Apr 04 2015 16:27
Your best option is to provide a Boxed_Value directly - if you only need it inside ChaiScript
Jason Turner
@lefticus
Apr 04 2015 16:37
If you construct a Boxed_Value from a shared_ptr<> it is smart enough to not double wrap it, it just stores the shared_ptr you passed in.
Victor Lopez
@VictorJL
Apr 04 2015 16:38
What I want is to register the base class and override it inside ChaiScript. Is that possible? I find no trouble by creating std::function's and then implementing them in ChaiScript, How can a Boxed_Value solve inheritance? create_scene takes only a type, and instantiates it, the constructors should not be filled or receive any parameter.
Jason Turner
@lefticus
Apr 04 2015 16:39
Object lifetime is mostly like you would expect it to be in C++. For instance, an object created inside of { } scope the object is destroyed when the scope exits - assuming you don't do anything to extend the lifetime of the object by storing it in some other datastructure, for instance.
regarding inheritance, just a second, let me find a link or two
Here's an example of how to emulate inheritance. https://gist.github.com/lefticus/65a6c23714537196a541 The problem is that for C++ to allow ChaiScript to inherit from a C++ class, you have to do some form of code generation. You have to make a proxy class (that gets compiled) that goes between ChaiScript and C++. I'd really like to make this better, but haven't come up with any good options yet.
Jason Turner
@lefticus
Apr 04 2015 16:44
If you get any ideas for how I could make this cleaner to use, let me know.
@arBmind fyi, I'm still playing with your performance questions
Andreas Reischuck
@arBmind
Apr 04 2015 16:52
@lefticus What is the purpose of these properties on each boxed_value
Jason Turner
@lefticus
Apr 04 2015 16:55
Are you referring to something in the example I just posted? or?
somethign else
Andreas Reischuck
@arBmind
Apr 04 2015 16:58
@lefticus I am doing some experimental implementation of the Boxed_Value
As far as I can see it, you capture double shared_ptr only for const_var
Oh I am wrong again :(
Jason Turner
@lefticus
Apr 04 2015 17:01
it should be that if you pass in a shared_ptr to var<> it passes that straight into the Data object in Boxed_Value. If you pass in shared_ptr to const_var, it casts that to const first, then passes it into the Data object. const var construction is here: https://github.com/ChaiScript/ChaiScript/blob/develop/include/chaiscript/dispatchkit/boxed_value.hpp#L337
I did do some experiments to reduce the number of boxing and unboxing and shared_ptr usage
the idea was to detect at compile time if you were passing in a "small" object that could be stored in a known amount of space. I was inspired by the std::string "small string optimization" which avoids dynamic allocations for strings <=15 chars or something
I ended up abandoning it after not seeing a huge performance difference, but left the branch around
Andreas Reischuck
@arBmind
Apr 04 2015 17:05
Have you ever thought about a small object allocator ?
Jason Turner
@lefticus
Apr 04 2015 17:05
I was just thinking about that yesterday, but haven't tried it
I have no experience with custom allocators
I do know that if use tcmalloc you can get a fair bit more performance
Jason Turner
@lefticus
Apr 04 2015 17:15
I think the main performance to be gained now is to simplify the actual execution path. So that function arguments don't have to be looked up repeatedly and that kind of thing, and loops are optimized and whatever. But I also don't have any great ideas on that yet
Everything I try to do right now, the compiler thwarts me. Almost every attempt in the past week has resulted in code that has more cache misses and is slower, even though to code looks to me like it should be simpler and faster.
Andreas Reischuck
@arBmind
Apr 04 2015 17:24
Maybe there is something unexpected going on...
Jason Turner
@lefticus
Apr 04 2015 17:27
well, I do have about another 10% gain sitting on the iife branch right now
but maybe there is, maybe some extra unexpected object copies or something
Victor Lopez
@VictorJL
Apr 04 2015 17:31
As from the C++ lounge in SO, they suggested libclang for C++ eval, this is for inheritance.
Jason Turner
@lefticus
Apr 04 2015 17:32
libclang can certainly do some amazing things.
it would be a rather large requirement to add to chaiscript
Victor Lopez
@VictorJL
Apr 04 2015 17:35
Matter of time and an experimental branch. It could help out the performance, still I have not used it neither know about libclang compilation under ARM platforms and how it performs.
Jason Turner
@lefticus
Apr 04 2015 17:37
I think with some helper classes and syntactic sugar I can make the inheritance story better
Victor Lopez
@VictorJL
Apr 04 2015 17:57
std::map<std::string, any> for data members? How are they being handled after constructing an object? Is it dynamic?
Jason Turner
@lefticus
Apr 04 2015 17:59
sorry, what's the context? For class defined inside of a ChaiScript, or for the map type inside of ChaiScript, or something else?
Victor Lopez
@VictorJL
Apr 04 2015 18:01
For the class defined inside of a ChaiScript, in many scripting languages you set properties with a dot, is that a feature in ChaiScript?
Jason Turner
@lefticus
Apr 04 2015 18:02
ah, it's not (yet) you want to talk to @arBmind he has a patch out that can do it to some degree
ChaiScript/ChaiScript#164
there was still a couple of questions about how exactly it might be implemented. His patch is for a method_missing equivalent to the ruby function
plus I've been focusing on performance
it'd be great if you wanted to read through that PR and comment on it
maybe try it out yourself too
Jason Turner
@lefticus
Apr 04 2015 18:09
I'll be away from the keyboard for a couple of hours. But I'm honestly surprised that ChaiScript is faster than perl for your tests @VictorJL PERL has had a LOT more time to become more refined and a LOT more people working on it
Andreas Reischuck
@arBmind
Apr 04 2015 19:14
@lefticus I read through the Boxed_Value code a bit further... It seems you are always triple pointering it.
Boxed_Value contains shared_ptr<Data> where Any always get's another shared_ptr<T> and stores it in a unique_ptr<Data>
Victor Lopez
@VictorJL
Apr 04 2015 19:19
@lefticus It is not that it's faster, it is more intuitive, it tells you exactly what do you want to deliver to the user with explicit parameters, and, for the operations done inside the engine it was required a scripting language only as a proxy.
The problem with Perl is that you're being required to embed the interpreter.
Andreas Reischuck
@arBmind
Apr 04 2015 19:22
@VictorJL it's the same for almost any scripting language... I also like it a lot.
Victor Lopez
@VictorJL
Apr 04 2015 19:27
I'm about to release the implementation of the project we're creating with ChaiScript as the front-end. I'll be sharing you the link here.
Victor Lopez
@VictorJL
Apr 04 2015 20:04
How to eval a .chai inside another .chai?
Andreas Reischuck
@arBmind
Apr 04 2015 20:14
use("other.chai") ?
Victor Lopez
@VictorJL
Apr 04 2015 20:20
And to add global objects?
Andreas Reischuck
@arBmind
Apr 04 2015 20:22
inside a script or from C++ ?
Victor Lopez
@VictorJL
Apr 04 2015 20:24
Inside a script I have 2 functions add_global and add_const_global, add_const_global would not allow me to modify the objects?
Victor Lopez
@VictorJL
Apr 04 2015 20:31
For example, I have this https://gist.github.com/VictorJL/ec9b938be5fdb4c66563 main.chai which will run the application.
And this title.chai which is the first scene https://gist.github.com/VictorJL/e021c0b906a005610abf
But when I run it tells me Error: "Error with function dispatch for function 'create_scene'" With parameters: (const Function).()
I guess it's because I'm using title.chai before adding the global.
Andreas Reischuck
@arBmind
Apr 04 2015 20:36
in chaiscript it's var instead of auto... or are these synonyms
Victor Lopez
@VictorJL
Apr 04 2015 20:42
auto and var seem to behave exactly, I've updated the scripts, actually, it's add_global_const
Andreas Reischuck
@arBmind
Apr 04 2015 20:45
from what i read, it should work
Victor Lopez
@VictorJL
Apr 04 2015 20:46
What's the difference betwee := and =?
*between.
Victor Lopez
@VictorJL
Apr 04 2015 20:57
title.init is a std::function<void()>, it does not assign to fun(){} what can I do inside .chai to assign these?
Jason Turner
@lefticus
Apr 04 2015 21:46
@arBmind @VictorJL auto and var are synonyms, we added auto after we ported to C++11, for some continuity with C++. := is reference assignment - it does not make a copy. You can also do auto &v = i again, for continuity with C++
Jason Turner
@lefticus
Apr 04 2015 21:52
I'm reading your code, I'm not sure why you are getting an error, I think I'd need to see more, but I have something I need to check
Victor Lopez
@VictorJL
Apr 04 2015 22:00
One thing I noticed is that you can't use variables from outside the scope so now I'm creating set_init, set_deinit and set_logic.
they wrap into a function that takes a shared_ptr to the object in question and then assign to init_impl, later on when we call init (in C++) we call init_impl(shared_from_this());
Jason Turner
@lefticus
Apr 04 2015 22:01
yes, functions have their own scope. You can use funmyvar{} to bind named locals, if you want to
oops
fun[myvar]() {}
like in C++, also
that binds by reference, since reference is behavior for params in chaiscript
Once you get this all committed, I'd like to give it a once over and see if there's anything I can help you clean up, or if there's anything I can add to ChaiScript to make your stuff more clean
Victor Lopez
@VictorJL
Apr 04 2015 22:05
I didn't know about the lambda capture, I'll test the code with the impl design and change it to funmyvar{} once it gets working.
Jason Turner
@lefticus
Apr 04 2015 22:06
there's no auto [&] capture working, I don't expect to ever implement that, but I might do something like fun[=myvar]() {} for capture by copy
Victor Lopez
@VictorJL
Apr 04 2015 22:10
I guess that it is working now.
Jason Turner
@lefticus
Apr 04 2015 22:17
Cool. I'll check it out as soon as I get the chance
Victor Lopez
@VictorJL
Apr 04 2015 22:17
If I do use("script.chai") will the objects declared inside that script alive?
will the objects delclared inside that script remain alive?
Jason Turner
@lefticus
Apr 04 2015 22:20
You'd want to do add_global if you want them to be accessible from other scripts
I do need to add a 'global' keyword to make that easier to use
oh!
also, the return value of a script is the last thing the script did
so you could do something like var somevalue = use("script.chai")
as an option
kind of like node.js in that way
Victor Lopez
@VictorJL
Apr 04 2015 22:24
Actually, I don't want them to remain alive since the engine gets stuck while doing nucleus.run() inside main.chai
Jason Turner
@lefticus
Apr 04 2015 22:24
ok, then they should get popped from the stack when the use() finishes
Victor Lopez
@VictorJL
Apr 04 2015 22:25
First add_global(nucleus, "g_nucleus") then use("scripts/scenes/title.chai") then nucleus.run()
after calling use("scripts/scenes/title.chai"), the objects inside title.chai will be popped from the memory stack and we will only preserve the last instruction made inside the script, right?
Jason Turner
@lefticus
Apr 04 2015 22:27
yes, should be
Victor Lopez
@VictorJL
Apr 04 2015 22:27
Excellent!
Jason Turner
@lefticus
Apr 04 2015 22:27
objects, yes. Functions created will still live. Lambdas == objects. so anything with def funcitonName() {} would persist
Jason Turner
@lefticus
Apr 04 2015 22:59
I feel like everything looks pretty good so far. You didn't have to jump though any major hoops except for the "add_global" thing