- Subject: Re: [slang-users] Using the tcc module
- From: Αγαθοκλής <aga.chatzimanikas@xxxxxxxxx>
- Date: Wed, 27 Jan 2021 10:58:46 +0200
Hi,
On Wed, Jan 27, at 07:22 Sebastian Gniazdowski wrote:
> On Sat, 23 Jan 2021 at 20:37, Αγαθοκλής <aga.chatzimanikas@xxxxxxxxx> wrote:
>
> > I wrote that code, mostly because i wanted constant String Type (char *)
> > objects
> > generated at runtime.
> >
>
> Could you elaborate, i.e.: why it isn't to do so with S-Lang only code?
Because there aren't const type objects, so any slang variable can be modified.
I guess that is why we are calling them variables. The only way is to use the
the C API, which allows to declare objects as read only. The thing is that many
times you do not know the value of an object untill runtime.
For instance this happens usually at initialization of an application, when you
need to sanitize paths, that is one reason, why the code, includes a realpath()
implementation, which i find it valuable.
We spoke with John, sometime ago, and he told me, that constant type objects will
be added in the core in the next major release, by introducing a "const" or "let"
keyword, to distinguish than regular declarations. I do not though about the scope
though, but usually such objects have global scope, as that way can not be garbage
collected. This is will be a very nice addition in my opinion.
> Sure, you can run any C code that tinycc can complile, but this is an
> > inherently
> > unsafe operation by default, since an access violation will crash the
> > interpreter.
> >
>
> I think that this is not a problem. It's up to the user to write a
> segfault-free C code, and that's not that hard.
I still do not like the idea, though in my editor i also bound tcc, so i could run
C code. The thing is that i didn't found a way to handle segfaults with a gracefully
way, unless you run it under an isolated environment, e.g., as John said by using
fork(). But then you need some kind of IPC, therefore can be complicated though
it seems doable, but complicated nevertheless.
But yes, some well tested code can be loaded at runtime, as they do not have to be
compiled as modules, as it plain source code.
Also it can be useful and for many applications, that need to recreate some tables
or so on.
> > > In general I would like to know how to run my first C code
> > (`printf("hi");`,
> > > probably) from within a S-Lang script.
> >
> > Assuming you've compiled the code, it is easy as that:
> >
> > slsh> () = evalfile ("./Tcc.sl");
> > slsh> variable s = Tcc.init ();
> > slsh> s.execute_string (`
> > #include <stdio.h>
> > int main (int argc, char **argv) {
> > return fprintf (stdout, "%s", argv[0]);
> > }`, ["my message\n"]);
> >
> > or if you want to add an intrinsic function:
> >
> > slsh> s.sladd_intrinsic_function ("myprint", 1, "c_myprint",
> > Integer_Type, [String_Type], `#include <stdio.h>
> > int c_myprint (char *msg) {return fprintf (stdout, "%s", msg);}`);
> > 0
> > slsh> myprint ("my message\n");
> >
>
> Thanks, I'll be adding the tcc support today, I will let you know how it
> went.
The code it needs some love though, as it is barely tested, but perhaps it
is the time, so thanks who are doing this, as this might bring the module in
a state that could be included in the mainstream.
> Be aware that you can not reuse a state, that's a tcc limitation, so you
> > have to
> > run s.delete () to free that state, and call the s.new () method to
> > instantiate it
> > again.
> >
>
> Does s.new() return some object, or does it initialize `s`?
No. It just that the TCC state has to be freed any time you reuse it.
You can do it in one step by using qualifiers, though:
s.new (;delete);
This will be re-usable and safe.
> Sebastian Gniazdowski
Regard, ag
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.
[2021 date index]
[2021 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]