Re: Modifying LJ to add a few math functions

  • From: Emil Dotchevski <emil@xxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 15 Aug 2013 18:38:15 -0700

On Thu, Jul 25, 2013 at 11:51 PM, Mike Pall <mike-1307@xxxxxxxxxx> wrote:
>
> Emil Dotchevski wrote:
> > What should I change in order to add functions to the math lib? I
> > added the functions to lib_math.c like so:
> >
> > LJLIB_ASM(math_nmod)  LJLIB_REC(math_nmod)
> > {
> >   int x = lj_lib_checkint(L, 1);
> >   if (L->base+1 < L->top) {
> >     int y = lj_lib_checkint(L, 2);
> >       setintV(L->base-1, x%y);
> >       return FFH_RES(1);
> >   }
> >   return FFH_RETRY;
> > }
>
> Change this to:
>
>   LJLIB_CF(math_nmod)
>   {
>     int x = lj_lib_checkint(L, 1);
>     int y = lj_lib_checkint(L, 2);
>     setintV(L->top-1, x%y);
>     return 1;
>   }
>
> > LJLIB_ASM(math_pi)            LJLIB_REC(math_pi)
> > {
>
> Umm ... is this really a function? Because math.pi is already
> defined. But it's a constant, not a function. Look for:
>
>   LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
>
> If you really, really want to turn that into a function, then
> remove that definition and replace it with:
>
>   LJLIB_CF(math_pi)
>   {
>     setnumV(L->top++,3.14159265358979323846);
>     return 1;
>   }
>
> [Note: this will only get you new functions for the interpreter.
> Ok, so that's all you need on a (restricted) console. Hooking into
> the JIT-compiler would be a bit more involved. This is just in case
> someone else reads this posting out of context.]

Mike, I'm pretty sure that I've broken the X360 build with this
change. Is adding the two LJLIB_CFs as you indicated above all I have
to do to add math.nmod/ndiv?

When I build, in the generated files I get nmod and ndiv, and they
appear just before math.deg and math.rad in lj_ffdef.h. And, math.rad
and math.deg seem messed up (the execution doesn't continue past the
point in the lua file where math.rad is called) but only on X360. I'm
guessing that some entries or indexes or tables don't match, and that
on PC it's still not working right, even though it doesn't crash.

It seems like lj_ffrecord.c isn't generated and I haven't added
anything to it for nmod/ndiv. Could that be my problem?

Thank you once again for looking into this!

Emil

Other related posts: