@@ -163,8 +163,10 @@ automatically.")
163163 (register push-light-user-data (Fn [&Lua (Ptr ())] ()) "lua_pushlightuserdata")
164164 (doc push-nil "Push nil onto the stack.")
165165 (register push-nil (Fn [&Lua] ()) "lua_pushnil")
166- (doc push-float "Push a float (Lua number) onto the stack.")
167- (register push-float (Fn [&Lua Float] ()) "lua_pushnumber")
166+ (doc push-double "Push a double (Lua number) onto the stack.")
167+ (register push-double (Fn [&Lua Double] ()) "lua_pushnumber")
168+ (doc push-float "Push a float onto the stack as a Lua number.")
169+ (defn push-float [lua f] (push-double lua (Double.from-float f)))
168170 (doc push-string "Push a C string onto the stack. For Carp strings, use
169171[`push-carp-str`](#push-carp-str) instead.")
170172 (deftemplate push-string
@@ -189,9 +191,13 @@ elements on the stack.")
189191 (doc get-int "Read the value at `index` as an integer. Does not check the
190192type—use [`Luax.maybe-get-int`](#maybe-get-int) for a safe version.")
191193 (register get-int (Fn [&Lua Int] Int) "lua_tointeger")
192- (doc get-float "Read the value at `index` as a float. Does not check the
194+ (doc get-double "Read the value at `index` as a double. Does not check the
195+ type—use [`Luax.maybe-get-double`](#maybe-get-double) for a safe version.")
196+ (register get-double (Fn [&Lua Int] Double) "lua_tonumber")
197+ (doc get-float "Read the value at `index` as a float. Lua numbers are doubles,
198+ so this narrows; use [`get-double`](#get-double) instead. Does not check the
193199type—use [`Luax.maybe-get-float`](#maybe-get-float) for a safe version.")
194- (register get-float (Fn [&Lua Int] Float) "lua_tonumber" )
200+ (defn get-float [lua index] (Double.to-float (get-double lua index)) )
195201 (doc get-string "Read the value at `index` as a C string pointer. Returns a
196202raw pointer; prefer [`Luax.get-carp-str`](#get-carp-str) or
197203[`Luax.maybe-get-string`](#maybe-get-string) for safe access.")
@@ -595,30 +601,35 @@ the Lua standard library available.
595601```
596602
597603Values are passed between Carp and Lua through the stack. Push values with
598- [`push-int`](#push-int), [`push-float `](#push-float ),
604+ [`push-int`](#push-int), [`push-double `](#push-double ),
599605[`push-bool`](#push-bool), [`push-carp-str`](#push-carp-str), etc. Read them
600- back with [`get-int`](#get-int), [`get-float `](#get-float ), and so on, using
606+ back with [`get-int`](#get-int), [`get-double `](#get-double ), and so on, using
601607negative indices to address from the top of the stack (-1 is the top element).
602608
603609```
604610; push two values, read the top one
605611(Lua.push-int lua 42)
606- (Lua.push-float lua 3.14f )
607- (let [f (Lua.get-float lua -1) ; 3.14
608- i (Lua.get-int lua -2)] ; 42
612+ (Lua.push-double lua 3.14 )
613+ (let [d (Lua.get-double lua -1) ; 3.14
614+ i (Lua.get-int lua -2)] ; 42
609615 (Lua.pop lua 2))
610616```
611617
618+ A Lua number is a C double. [`push-float`](#push-float) widens exactly, but
619+ [`get-float`](#get-float) narrows on the way back, so a number read through it
620+ loses everything past single precision and anything above the float range comes
621+ back as an infinity.
622+
612623To call a Lua function at the low level, push the function, then its arguments,
613624then use [`call`](#call) with the argument and result counts. The result
614625replaces the function and arguments on the stack.
615626
616627```
617628(Lua.get-global lua (cstr \"math\"))
618629(ignore (Lua.get-field lua -1 (cstr \"sqrt\")))
619- (Lua.push-float lua 9.0f )
630+ (Lua.push-double lua 9.0 )
620631(ignore (Lua.call lua 1 1 0))
621- (IO.println &(str (Lua.get-float lua -1))) ; 3.0
632+ (IO.println &(str (Lua.get-double lua -1))) ; 3.0
622633(Lua.pop lua 2) ; pop result and math table
623634```
624635
@@ -782,6 +793,11 @@ and assigns it to a global in one expression:
782793 (String.from-cstr-or (Lua.to-string lua index) @""))
783794
784795 (luax--def-maybe-get int Lua.TYPE_NUMBER Lua.get-int "an integer" "a number")
796+ (luax--def-maybe-get double
797+ Lua.TYPE_NUMBER
798+ Lua.get-double
799+ "a double"
800+ "a number")
785801 (luax--def-maybe-get float Lua.TYPE_NUMBER Lua.get-float "a float" "a number")
786802 (luax--def-maybe-get bool
787803 Lua.TYPE_BOOLEAN
@@ -804,6 +820,7 @@ unchanged.")
804820 (Maybe.Nothing))))
805821
806822 (luax--def-set-global int Lua.push-int)
823+ (luax--def-set-global double Lua.push-double)
807824 (luax--def-set-global float Lua.push-float)
808825 (luax--def-set-global bool Lua.push-bool)
809826 ; `string` is defined explicitly rather than via luax--def-set-global: the
@@ -814,6 +831,11 @@ unchanged.")
814831 (do (Lua.push-carp-str lua value) (Lua.set-global lua (cstr name))))
815832
816833 (luax--def-get-global int Lua.TYPE_NUMBER Lua.get-int "an integer" "a number")
834+ (luax--def-get-global double
835+ Lua.TYPE_NUMBER
836+ Lua.get-double
837+ "a double"
838+ "a number")
817839 (luax--def-get-global float Lua.TYPE_NUMBER Lua.get-float "a float" "a number")
818840 (luax--def-get-global bool
819841 Lua.TYPE_BOOLEAN
@@ -833,11 +855,17 @@ unchanged.")
833855 result)))
834856
835857 (luax--def-set-field int Lua.push-int)
858+ (luax--def-set-field double Lua.push-double)
836859 (luax--def-set-field float Lua.push-float)
837860 (luax--def-set-field bool Lua.push-bool)
838861 (luax--def-set-field string Lua.push-carp-str)
839862
840863 (luax--def-get-field int Lua.TYPE_NUMBER Lua.get-int "an integer" "a number")
864+ (luax--def-get-field double
865+ Lua.TYPE_NUMBER
866+ Lua.get-double
867+ "a double"
868+ "a number")
841869 (luax--def-get-field float Lua.TYPE_NUMBER Lua.get-float "a float" "a number")
842870 (luax--def-get-field bool
843871 Lua.TYPE_BOOLEAN
0 commit comments