You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

native.rs 103KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589
  1. // Copyright (C) 2019 Inderjit Gill
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. use crate::bitmap;
  13. use crate::colour::{Colour, ColourFormat, ColourPreset, ProcColourStateStruct};
  14. use crate::context::Context;
  15. use crate::ease::easing_from_keyword;
  16. use crate::error::Error;
  17. use crate::focal;
  18. use crate::iname::Iname;
  19. use crate::interp;
  20. use crate::keywords::Keyword;
  21. use crate::mathutil;
  22. use crate::packable::{Mule, Packable};
  23. use crate::path;
  24. use crate::prng;
  25. use crate::program::Program;
  26. use crate::repeat;
  27. use crate::result::Result;
  28. use crate::vm::{StackPeek, Var, Vm};
  29. use crate::uvmapper::BrushType;
  30. use std::cell::{RefCell, RefMut};
  31. use std::collections::HashMap;
  32. use std::rc::Rc;
  33. use strum::IntoEnumIterator;
  34. use strum_macros::{Display, EnumIter, EnumString};
  35. use log::error;
  36. #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Display, EnumString, EnumIter)]
  37. pub enum Native {
  38. #[strum(serialize = "UnreachableNativeStart")]
  39. NativeStart = Keyword::KeywordEnd as isize,
  40. // // misc
  41. // //
  42. // #[strum(serialize = "debug/print")]
  43. // DebugPrint,
  44. #[strum(serialize = "nth")]
  45. Nth,
  46. #[strum(serialize = "vector/length")]
  47. VectorLength,
  48. #[strum(serialize = "probe")]
  49. Probe,
  50. #[strum(serialize = "meta")]
  51. Meta,
  52. // shapes
  53. //
  54. #[strum(serialize = "line")]
  55. Line,
  56. #[strum(serialize = "rect")]
  57. Rect,
  58. #[strum(serialize = "circle")]
  59. Circle,
  60. #[strum(serialize = "circle-slice")]
  61. CircleSlice,
  62. #[strum(serialize = "poly")]
  63. Poly,
  64. #[strum(serialize = "quadratic")]
  65. Quadratic,
  66. #[strum(serialize = "bezier")]
  67. Bezier,
  68. #[strum(serialize = "bezier-bulging")]
  69. BezierBulging,
  70. #[strum(serialize = "stroked-bezier")]
  71. StrokedBezier,
  72. #[strum(serialize = "stroked-bezier-rect")]
  73. StrokedBezierRect,
  74. // matrix transforms
  75. //
  76. #[strum(serialize = "__matrix_push")]
  77. MatrixPush, // special native function invoked by the compiler for on-matrix-stack
  78. #[strum(serialize = "__matrix_pop")]
  79. MatrixPop, // special native function invoked by the compiler for on-matrix-stack
  80. #[strum(serialize = "translate")]
  81. Translate,
  82. #[strum(serialize = "rotate")]
  83. Rotate,
  84. #[strum(serialize = "scale")]
  85. Scale,
  86. // colour
  87. //
  88. #[strum(serialize = "col/convert")]
  89. ColConvert,
  90. #[strum(serialize = "__colour_constructor_start")]
  91. ColConstructorStart_, // Special Enums required by the compiler to recognise colour constructors
  92. #[strum(serialize = "col/rgb")]
  93. ColRGB,
  94. #[strum(serialize = "col/hsl")]
  95. ColHSL,
  96. #[strum(serialize = "col/hsluv")]
  97. ColHSLuv,
  98. #[strum(serialize = "col/hsv")]
  99. ColHSV,
  100. #[strum(serialize = "col/lab")]
  101. ColLAB,
  102. #[strum(serialize = "__colour_constructor_end")]
  103. ColConstructorEnd_, // Special Enums required by the compiler to recognise colour constructors
  104. #[strum(serialize = "col/complementary")]
  105. ColComplementary,
  106. #[strum(serialize = "col/split-complementary")]
  107. ColSplitComplementary,
  108. #[strum(serialize = "col/analagous")]
  109. ColAnalagous,
  110. #[strum(serialize = "col/triad")]
  111. ColTriad,
  112. #[strum(serialize = "col/darken")]
  113. ColDarken,
  114. #[strum(serialize = "col/lighten")]
  115. ColLighten,
  116. #[strum(serialize = "col/set-alpha")]
  117. ColSetAlpha,
  118. #[strum(serialize = "col/get-alpha")]
  119. ColGetAlpha,
  120. #[strum(serialize = "col/set-r")]
  121. ColSetR,
  122. #[strum(serialize = "col/get-r")]
  123. ColGetR,
  124. #[strum(serialize = "col/set-g")]
  125. ColSetG,
  126. #[strum(serialize = "col/get-g")]
  127. ColGetG,
  128. #[strum(serialize = "col/set-b")]
  129. ColSetB,
  130. #[strum(serialize = "col/get-b")]
  131. ColGetB,
  132. #[strum(serialize = "col/set-h")]
  133. ColSetH,
  134. #[strum(serialize = "col/get-h")]
  135. ColGetH,
  136. #[strum(serialize = "col/set-s")]
  137. ColSetS,
  138. #[strum(serialize = "col/get-s")]
  139. ColGetS,
  140. #[strum(serialize = "col/set-l")]
  141. ColSetL,
  142. #[strum(serialize = "col/get-l")]
  143. ColGetL,
  144. #[strum(serialize = "col/set-a")]
  145. ColSetA,
  146. #[strum(serialize = "col/get-a")]
  147. ColGetA,
  148. #[strum(serialize = "col/set-v")]
  149. ColSetV,
  150. #[strum(serialize = "col/get-v")]
  151. ColGetV,
  152. #[strum(serialize = "col/build-procedural")]
  153. ColBuildProcedural,
  154. // #[strum(serialize = "col/build-bezier")]
  155. // ColBuildBezier,
  156. #[strum(serialize = "col/value")]
  157. ColValue,
  158. // math
  159. //
  160. #[strum(serialize = "math/distance")]
  161. MathDistance,
  162. #[strum(serialize = "math/normal")]
  163. MathNormal,
  164. #[strum(serialize = "math/clamp")]
  165. MathClamp,
  166. #[strum(serialize = "math/radians->degrees")]
  167. MathRadiansDegrees,
  168. #[strum(serialize = "math/cos")]
  169. MathCos,
  170. #[strum(serialize = "math/sin")]
  171. MathSin,
  172. // prng
  173. //
  174. #[strum(serialize = "prng/build")]
  175. PrngBuild,
  176. #[strum(serialize = "prng/values")]
  177. PrngValues,
  178. #[strum(serialize = "prng/value")]
  179. PrngValue,
  180. #[strum(serialize = "prng/perlin")]
  181. PrngPerlin,
  182. // interp
  183. //
  184. #[strum(serialize = "interp/build")]
  185. InterpBuild,
  186. #[strum(serialize = "interp/value")]
  187. InterpValue,
  188. #[strum(serialize = "interp/cos")]
  189. InterpCos,
  190. #[strum(serialize = "interp/sin")]
  191. InterpSin,
  192. #[strum(serialize = "interp/bezier")]
  193. InterpBezier,
  194. #[strum(serialize = "interp/bezier-tangent")]
  195. InterpBezierTangent,
  196. #[strum(serialize = "interp/ray")]
  197. InterpRay,
  198. #[strum(serialize = "interp/line")]
  199. InterpLine,
  200. #[strum(serialize = "interp/circle")]
  201. InterpCircle,
  202. // path
  203. //
  204. #[strum(serialize = "path/linear")]
  205. PathLinear,
  206. #[strum(serialize = "path/circle")]
  207. PathCircle,
  208. #[strum(serialize = "path/spline")]
  209. PathSpline,
  210. #[strum(serialize = "path/bezier")]
  211. PathBezier,
  212. // repeat
  213. //
  214. #[strum(serialize = "repeat/symmetry-vertical")]
  215. RepeatSymmetryVertical,
  216. #[strum(serialize = "repeat/symmetry-horizontal")]
  217. RepeatSymmetryHorizontal,
  218. #[strum(serialize = "repeat/symmetry-4")]
  219. RepeatSymmetry4,
  220. #[strum(serialize = "repeat/symmetry-8")]
  221. RepeatSymmetry8,
  222. #[strum(serialize = "repeat/rotate")]
  223. RepeatRotate,
  224. #[strum(serialize = "repeat/rotate-mirrored")]
  225. RepeatRotateMirrored,
  226. // focal
  227. //
  228. #[strum(serialize = "focal/build-point")]
  229. FocalBuildPoint,
  230. #[strum(serialize = "focal/build-vline")]
  231. FocalBuildVLine,
  232. #[strum(serialize = "focal/build-hline")]
  233. FocalBuildHLine,
  234. #[strum(serialize = "focal/value")]
  235. FocalValue,
  236. // bitmap
  237. //
  238. #[strum(serialize = "bitmap/each")]
  239. BitmapEach,
  240. #[strum(serialize = "bitmap/value")]
  241. BitmapValue,
  242. #[strum(serialize = "bitmap/width")]
  243. BitmapWidth,
  244. #[strum(serialize = "bitmap/height")]
  245. BitmapHeight,
  246. // gen
  247. //
  248. #[strum(serialize = "gen/stray-int")]
  249. GenStrayInt,
  250. #[strum(serialize = "gen/stray")]
  251. GenStray,
  252. #[strum(serialize = "gen/stray-2d")]
  253. GenStray2D,
  254. #[strum(serialize = "gen/stray-3d")]
  255. GenStray3D,
  256. #[strum(serialize = "gen/stray-4d")]
  257. GenStray4D,
  258. #[strum(serialize = "gen/int")]
  259. GenInt,
  260. #[strum(serialize = "gen/scalar")]
  261. GenScalar,
  262. #[strum(serialize = "gen/2d")]
  263. Gen2D,
  264. #[strum(serialize = "gen/select")]
  265. GenSelect,
  266. #[strum(serialize = "gen/col")]
  267. GenCol,
  268. #[strum(serialize = "UnreachableNativeEnd")]
  269. NativeEnd,
  270. }
  271. impl Packable for Native {
  272. fn pack(&self, cursor: &mut String) -> Result<()> {
  273. Mule::pack_label(cursor, &self.to_string());
  274. Ok(())
  275. }
  276. fn unpack(cursor: &str) -> Result<(Self, &str)> {
  277. let ns = Mule::next_space(cursor);
  278. let sub = &cursor[0..ns];
  279. let res = sub.parse::<Native>()?;
  280. Ok((res, &cursor[ns..]))
  281. }
  282. }
  283. // return a tuple
  284. // .0 == input arguments as a vector of (name, default value) pairs
  285. // .1 == how the native function affects the vm's stack in terms of opcode offset
  286. //
  287. pub fn parameter_info(native: Native) -> Result<(Vec<(Keyword, Var)>, i32)> {
  288. match native {
  289. // misc
  290. Native::Nth => nth_parameter_info(),
  291. Native::VectorLength => vector_length_parameter_info(),
  292. Native::Probe => probe_parameter_info(),
  293. Native::Meta => meta_parameter_info(),
  294. // shapes
  295. Native::Line => line_parameter_info(),
  296. Native::Rect => rect_parameter_info(),
  297. Native::Circle => circle_parameter_info(),
  298. Native::CircleSlice => circle_slice_parameter_info(),
  299. Native::Poly => poly_parameter_info(),
  300. Native::Quadratic => quadratic_parameter_info(),
  301. Native::Bezier => bezier_parameter_info(),
  302. Native::BezierBulging => bezier_bulging_parameter_info(),
  303. Native::StrokedBezier => stroked_bezier_parameter_info(),
  304. Native::StrokedBezierRect => stroked_bezier_rect_parameter_info(),
  305. // transforms
  306. Native::MatrixPush => matrix_push_parameter_info(),
  307. Native::MatrixPop => matrix_pop_parameter_info(),
  308. Native::Translate => translate_parameter_info(),
  309. Native::Rotate => rotate_parameter_info(),
  310. Native::Scale => scale_parameter_info(),
  311. // colour
  312. Native::ColConvert => col_convert_parameter_info(),
  313. Native::ColRGB => col_rgb_parameter_info(),
  314. Native::ColHSL => col_hsl_parameter_info(),
  315. Native::ColHSLuv => col_hsluv_parameter_info(),
  316. Native::ColHSV => col_hsv_parameter_info(),
  317. Native::ColLAB => col_lab_parameter_info(),
  318. Native::ColComplementary => col_complementary_parameter_info(),
  319. Native::ColSplitComplementary => col_split_complementary_parameter_info(),
  320. Native::ColAnalagous => col_analagous_parameter_info(),
  321. Native::ColTriad => col_triad_parameter_info(),
  322. Native::ColDarken => common_colour_value_parameter_info(),
  323. Native::ColLighten => common_colour_value_parameter_info(),
  324. Native::ColSetAlpha => common_colour_value_parameter_info(),
  325. Native::ColGetAlpha => common_colour_only_parameter_info(),
  326. Native::ColSetR => common_colour_value_parameter_info(),
  327. Native::ColGetR => common_colour_only_parameter_info(),
  328. Native::ColSetG => common_colour_value_parameter_info(),
  329. Native::ColGetG => common_colour_only_parameter_info(),
  330. Native::ColSetB => common_colour_value_parameter_info(),
  331. Native::ColGetB => common_colour_only_parameter_info(),
  332. Native::ColSetH => common_colour_value_parameter_info(),
  333. Native::ColGetH => common_colour_only_parameter_info(),
  334. Native::ColSetS => common_colour_value_parameter_info(),
  335. Native::ColGetS => common_colour_only_parameter_info(),
  336. Native::ColSetL => common_colour_value_parameter_info(),
  337. Native::ColGetL => common_colour_only_parameter_info(),
  338. Native::ColSetA => common_colour_value_parameter_info(),
  339. Native::ColGetA => common_colour_only_parameter_info(),
  340. Native::ColSetV => common_colour_value_parameter_info(),
  341. Native::ColGetV => common_colour_only_parameter_info(),
  342. Native::ColBuildProcedural => col_build_procedural_parameter_info(),
  343. Native::ColValue => col_value_parameter_info(),
  344. // math
  345. Native::MathDistance => math_distance_parameter_info(),
  346. Native::MathNormal => math_normal_parameter_info(),
  347. Native::MathClamp => math_clamp_parameter_info(),
  348. Native::MathRadiansDegrees => math_radians_degrees_parameter_info(),
  349. Native::MathCos => math_cos_parameter_info(),
  350. Native::MathSin => math_sin_parameter_info(),
  351. // prng
  352. Native::PrngBuild => prng_build_parameter_info(),
  353. Native::PrngValues => prng_values_parameter_info(),
  354. Native::PrngValue => prng_value_parameter_info(),
  355. Native::PrngPerlin => prng_perlin_parameter_info(),
  356. // interp
  357. Native::InterpBuild => interp_build_parameter_info(),
  358. Native::InterpValue => interp_value_parameter_info(),
  359. Native::InterpCos => interp_cos_parameter_info(),
  360. Native::InterpSin => interp_sin_parameter_info(),
  361. Native::InterpBezier => interp_bezier_parameter_info(),
  362. Native::InterpBezierTangent => interp_bezier_tangent_parameter_info(),
  363. Native::InterpRay => interp_ray_parameter_info(),
  364. Native::InterpLine => interp_line_parameter_info(),
  365. Native::InterpCircle => interp_circle_parameter_info(),
  366. // path
  367. Native::PathLinear => path_linear_parameter_info(),
  368. Native::PathCircle => path_circle_parameter_info(),
  369. Native::PathSpline => path_spline_parameter_info(),
  370. Native::PathBezier => path_bezier_parameter_info(),
  371. // repeat
  372. Native::RepeatSymmetryVertical => repeat_symmetry_vertical_parameter_info(),
  373. Native::RepeatSymmetryHorizontal => repeat_symmetry_horizontal_parameter_info(),
  374. Native::RepeatSymmetry4 => repeat_symmetry_4_parameter_info(),
  375. Native::RepeatSymmetry8 => repeat_symmetry_8_parameter_info(),
  376. Native::RepeatRotate => repeat_rotate_parameter_info(),
  377. Native::RepeatRotateMirrored => repeat_rotate_mirrored_parameter_info(),
  378. // focal
  379. Native::FocalBuildPoint => focal_build_generic_parameter_info(),
  380. Native::FocalBuildVLine => focal_build_generic_parameter_info(),
  381. Native::FocalBuildHLine => focal_build_generic_parameter_info(),
  382. Native::FocalValue => focal_value_parameter_info(),
  383. // bitmap
  384. Native::BitmapEach => bitmap_each_parameter_info(),
  385. Native::BitmapValue => bitmap_value_parameter_info(),
  386. Native::BitmapWidth => bitmap_width_parameter_info(),
  387. Native::BitmapHeight => bitmap_height_parameter_info(),
  388. // gen
  389. Native::GenStrayInt => gen_stray_int_parameter_info(),
  390. Native::GenStray => gen_stray_parameter_info(),
  391. Native::GenStray2D => gen_stray_2d_parameter_info(),
  392. Native::GenStray3D => gen_stray_3d_parameter_info(),
  393. Native::GenStray4D => gen_stray_4d_parameter_info(),
  394. Native::GenInt => gen_int_parameter_info(),
  395. Native::GenScalar => gen_scalar_parameter_info(),
  396. Native::Gen2D => gen_2d_parameter_info(),
  397. Native::GenSelect => gen_select_parameter_info(),
  398. Native::GenCol => gen_col_parameter_info(),
  399. _ => {
  400. error!("parameter_info");
  401. Err(Error::Native)
  402. }
  403. }
  404. }
  405. pub fn execute_native(
  406. vm: &mut Vm,
  407. context: &mut Context,
  408. program: &Program,
  409. native: Native,
  410. ) -> Result<Option<Var>> {
  411. match native {
  412. // misc
  413. Native::Nth => nth_execute(vm),
  414. Native::VectorLength => vector_length_execute(vm),
  415. Native::Probe => probe_execute(vm, context),
  416. Native::Meta => meta_execute(vm, context),
  417. // shapes
  418. Native::Line => line_execute(vm, context),
  419. Native::Rect => rect_execute(vm, context),
  420. Native::Circle => circle_execute(vm, context),
  421. Native::CircleSlice => circle_slice_execute(vm, context),
  422. Native::Poly => poly_execute(vm, context),
  423. Native::Quadratic => quadratic_execute(vm, context),
  424. Native::Bezier => bezier_execute(vm, context),
  425. Native::BezierBulging => bezier_bulging_execute(vm, context),
  426. Native::StrokedBezier => stroked_bezier_execute(vm, context),
  427. Native::StrokedBezierRect => stroked_bezier_rect_execute(vm, context),
  428. // transforms
  429. Native::MatrixPush => matrix_push_execute(vm, context),
  430. Native::MatrixPop => matrix_pop_execute(vm, context),
  431. Native::Translate => translate_execute(vm, context),
  432. Native::Rotate => rotate_execute(vm, context),
  433. Native::Scale => scale_execute(vm, context),
  434. // colours
  435. Native::ColConvert => col_convert_execute(vm),
  436. Native::ColRGB => col_rgb_execute(vm),
  437. Native::ColHSL => col_hsl_execute(vm),
  438. Native::ColHSLuv => col_hsluv_execute(vm),
  439. Native::ColHSV => col_hsv_execute(vm),
  440. Native::ColLAB => col_lab_execute(vm),
  441. Native::ColComplementary => col_complementary_execute(vm),
  442. Native::ColSplitComplementary => col_split_complementary_execute(vm),
  443. Native::ColAnalagous => col_analagous_execute(vm),
  444. Native::ColTriad => col_triad_execute(vm),
  445. Native::ColDarken => col_darken_execute(vm),
  446. Native::ColLighten => col_lighten_execute(vm),
  447. Native::ColSetAlpha => col_set_elem_execute(vm, 3),
  448. Native::ColGetAlpha => col_get_elem_execute(vm, 3),
  449. Native::ColSetR => col_set_elem_execute(vm, 0),
  450. Native::ColGetR => col_get_elem_execute(vm, 0),
  451. Native::ColSetG => col_set_elem_execute(vm, 1),
  452. Native::ColGetG => col_get_elem_execute(vm, 1),
  453. Native::ColSetB => col_set_elem_execute(vm, 2),
  454. Native::ColGetB => col_get_elem_execute(vm, 2),
  455. Native::ColSetH => col_set_elem_execute(vm, 0),
  456. Native::ColGetH => col_get_elem_execute(vm, 0),
  457. Native::ColSetS => col_set_elem_execute(vm, 1),
  458. Native::ColGetS => col_get_elem_execute(vm, 1),
  459. Native::ColSetL => col_set_elem_execute(vm, 2), // bug: will fail when called for LAB colour
  460. Native::ColGetL => col_get_elem_execute(vm, 2),
  461. Native::ColSetA => col_set_elem_execute(vm, 1),
  462. Native::ColGetA => col_get_elem_execute(vm, 1),
  463. Native::ColSetV => col_set_elem_execute(vm, 2),
  464. Native::ColGetV => col_get_elem_execute(vm, 2),
  465. Native::ColBuildProcedural => col_build_procedural_execute(vm),
  466. Native::ColValue => col_value_execute(vm),
  467. // math
  468. Native::MathDistance => math_distance_execute(vm),
  469. Native::MathNormal => math_normal_execute(vm),
  470. Native::MathClamp => math_clamp_execute(vm),
  471. Native::MathRadiansDegrees => math_radians_degrees_execute(vm),
  472. Native::MathCos => math_cos_execute(vm),
  473. Native::MathSin => math_sin_execute(vm),
  474. // prng
  475. Native::PrngBuild => prng_build_execute(vm),
  476. Native::PrngValues => prng_values_execute(vm),
  477. Native::PrngValue => prng_value_execute(vm),
  478. Native::PrngPerlin => prng_perlin_execute(vm),
  479. // interp
  480. Native::InterpBuild => interp_build_execute(vm),
  481. Native::InterpValue => interp_value_execute(vm),
  482. Native::InterpCos => interp_cos_execute(vm),
  483. Native::InterpSin => interp_sin_execute(vm),
  484. Native::InterpBezier => interp_bezier_execute(vm),
  485. Native::InterpBezierTangent => interp_bezier_tangent_execute(vm),
  486. Native::InterpRay => interp_ray_execute(vm),
  487. Native::InterpLine => interp_line_execute(vm),
  488. Native::InterpCircle => interp_circle_execute(vm),
  489. // path
  490. Native::PathLinear => path_linear_execute(vm, context, program),
  491. Native::PathCircle => path_circle_execute(vm, context, program),
  492. Native::PathSpline => path_spline_execute(vm, context, program),
  493. Native::PathBezier => path_bezier_execute(vm, context, program),
  494. // repeat
  495. Native::RepeatSymmetryVertical => repeat_symmetry_vertical_execute(vm, context, program),
  496. Native::RepeatSymmetryHorizontal => {
  497. repeat_symmetry_horizontal_execute(vm, context, program)
  498. }
  499. Native::RepeatSymmetry4 => repeat_symmetry_4_execute(vm, context, program),
  500. Native::RepeatSymmetry8 => repeat_symmetry_8_execute(vm, context, program),
  501. Native::RepeatRotate => repeat_rotate_execute(vm, context, program),
  502. Native::RepeatRotateMirrored => repeat_rotate_mirrored_execute(vm, context, program),
  503. // focal
  504. Native::FocalBuildPoint => focal_build_point_execute(vm),
  505. Native::FocalBuildVLine => focal_build_vline_execute(vm),
  506. Native::FocalBuildHLine => focal_build_hline_execute(vm),
  507. Native::FocalValue => focal_value_execute(vm, context),
  508. // bitmap
  509. Native::BitmapEach => bitmap_each_execute(vm, context, program),
  510. Native::BitmapValue => bitmap_value_execute(vm, context, program),
  511. Native::BitmapWidth => bitmap_width_execute(vm, context, program),
  512. Native::BitmapHeight => bitmap_height_execute(vm, context, program),
  513. // gen
  514. Native::GenStrayInt => gen_stray_int_execute(vm),
  515. Native::GenStray => gen_stray_execute(vm),
  516. Native::GenStray2D => gen_stray_2d_execute(vm),
  517. Native::GenStray3D => gen_stray_3d_execute(vm),
  518. Native::GenStray4D => gen_stray_4d_execute(vm),
  519. Native::GenInt => gen_int_execute(vm),
  520. Native::GenScalar => gen_scalar_execute(vm),
  521. Native::Gen2D => gen_2d_execute(vm),
  522. Native::GenSelect => gen_select_execute(vm),
  523. Native::GenCol => gen_col_execute(vm),
  524. _ => {
  525. error!("execute_native");
  526. Err(Error::Native)
  527. }
  528. }
  529. }
  530. pub fn name_to_native_hash() -> HashMap<Iname, Native> {
  531. let mut hm: HashMap<Iname, Native> = HashMap::new();
  532. for n in Native::iter() {
  533. hm.insert(Iname::from(n), n);
  534. }
  535. hm
  536. }
  537. fn is_arg_given(bits: i32, position: usize) -> bool {
  538. // note: the position value will corresspond to the stack_peek value.
  539. // stack peek values start at 1 rather than 0 (since the current stack
  540. // pointer points to the 'next' free stack location). therefore we'll
  541. // need to subtract one from the position value
  542. (bits & (1 << (position - 1))) == 0
  543. }
  544. // can't have this as a member of Vm thanks to the borrow checker
  545. fn stack_peek_vars(stack: &[Var], sp: usize, offset: usize) -> Result<&Vec<Var>> {
  546. if let Var::Vector(vs) = &stack[sp - offset] {
  547. Ok(vs)
  548. } else {
  549. error!("expected Var::Vector");
  550. return Err(Error::Native);
  551. }
  552. }
  553. fn read_brush(kw: Keyword) -> BrushType {
  554. match kw {
  555. Keyword::BrushFlat => BrushType::Flat,
  556. Keyword::BrushA => BrushType::A,
  557. Keyword::BrushB => BrushType::B,
  558. Keyword::BrushC => BrushType::C,
  559. Keyword::BrushD => BrushType::D,
  560. Keyword::BrushE => BrushType::E,
  561. Keyword::BrushF => BrushType::F,
  562. Keyword::BrushG => BrushType::G,
  563. _ => BrushType::Flat,
  564. }
  565. }
  566. fn stack_peek_proc_colour_state_struct(
  567. stack: &[Var],
  568. sp: usize,
  569. offset: usize,
  570. ) -> Result<&ProcColourStateStruct> {
  571. if let Var::ProcColourState(pcss) = &stack[sp - offset] {
  572. Ok(pcss)
  573. } else {
  574. error!("expected Var::ProcColourState");
  575. return Err(Error::Native);
  576. }
  577. }
  578. fn stack_peek_interp_state_struct(
  579. stack: &[Var],
  580. sp: usize,
  581. offset: usize,
  582. ) -> Result<&interp::InterpStateStruct> {
  583. if let Var::InterpState(iss) = &stack[sp - offset] {
  584. Ok(iss)
  585. } else {
  586. error!("expected Var::InterpState");
  587. return Err(Error::Native);
  588. }
  589. }
  590. fn stack_peek_focal_state_struct(
  591. stack: &[Var],
  592. sp: usize,
  593. offset: usize,
  594. ) -> Result<&focal::FocalStateStruct> {
  595. if let Var::FocalState(fss) = &stack[sp - offset] {
  596. Ok(fss)
  597. } else {
  598. error!("expected Var::FocalState");
  599. return Err(Error::Native);
  600. }
  601. }
  602. fn ref_mut_prng_state_struct(
  603. stack: &[Var],
  604. sp: usize,
  605. offset: usize,
  606. ) -> Result<RefMut<prng::PrngStateStruct>> {
  607. if let Var::PrngState(prng_state_struct) = &stack[sp - offset] {
  608. Ok(prng_state_struct.borrow_mut())
  609. } else {
  610. error!("expected Var::PrngState");
  611. return Err(Error::Native);
  612. }
  613. }
  614. fn nth_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  615. Ok((
  616. // input arguments
  617. vec![
  618. (Keyword::From, Var::Bool(false)),
  619. (Keyword::N, Var::Float(0.0)),
  620. ],
  621. // stack offset
  622. 1,
  623. ))
  624. }
  625. fn nth_execute(vm: &mut Vm) -> Result<Option<Var>> {
  626. let default_mask: i32 = vm.stack_peek(3)?;
  627. // require a 'from' argument
  628. if !is_arg_given(default_mask, 1) {
  629. error!("nth requires from parameter");
  630. return Err(Error::Native);
  631. }
  632. // require an 'n' argument
  633. if !is_arg_given(default_mask, 2) {
  634. error!("nth requires n parameter");
  635. return Err(Error::Native);
  636. }
  637. let n: usize = vm.stack_peek(2)?;
  638. // from is either a Vector or a V2D
  639. let from_offset = 1;
  640. let res = match &vm.stack[vm.sp - from_offset] {
  641. Var::Vector(vs) => {
  642. if let Some(nth) = vs.get(n) {
  643. // optimisation: most of the values will be floats
  644. // or v2d so try and avoid calling clone
  645. match nth {
  646. Var::Float(f) => Some(Var::Float(*f)),
  647. Var::V2D(x, y) => Some(Var::V2D(*x, *y)),
  648. // todo: try and get rid of the clone call to nth
  649. _ => Some(nth.clone()),
  650. }
  651. } else {
  652. error!("nth: n out of range");
  653. return Err(Error::Native);
  654. }
  655. }
  656. Var::V2D(x, y) => match n {
  657. 0 => Some(Var::Float(*x)),
  658. 1 => Some(Var::Float(*y)),
  659. _ => {
  660. error!("nth indexing V2D out of range");
  661. return Err(Error::Native);
  662. }
  663. },
  664. _ => {
  665. error!("nth only accepts Vector or V2D in from parameter");
  666. return Err(Error::Native);
  667. }
  668. };
  669. Ok(res)
  670. }
  671. fn vector_length_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  672. Ok((
  673. // input arguments
  674. vec![(Keyword::Vector, Var::Bool(false))],
  675. // stack offset
  676. 1,
  677. ))
  678. }
  679. fn vector_length_execute(vm: &mut Vm) -> Result<Option<Var>> {
  680. let default_mask: i32 = vm.stack_peek(2)?;
  681. let vector_offset = 1;
  682. // require a 'vector' argument
  683. if !is_arg_given(default_mask, vector_offset) {
  684. error!("vector/length requires vector parameter");
  685. return Err(Error::Native);
  686. }
  687. // vector is either a Vector or a V2D
  688. let res = match &vm.stack[vm.sp - vector_offset] {
  689. Var::Vector(vs) => Some(Var::Int(vs.len() as i32)),
  690. Var::V2D(_, _) => Some(Var::Int(2)),
  691. _ => {
  692. error!("vector/length only accepts Vector or V2D in 'vector' parameter");
  693. return Err(Error::Native);
  694. }
  695. };
  696. Ok(res)
  697. }
  698. fn probe_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  699. Ok((
  700. // input arguments
  701. vec![
  702. (Keyword::Scalar, Var::Float(0.0)),
  703. (Keyword::Vector, Var::V2D(0.0, 0.0)),
  704. (Keyword::WorldSpace, Var::V2D(0.0, 0.0)),
  705. ],
  706. // stack offset
  707. 0,
  708. ))
  709. }
  710. fn probe_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  711. let default_mask: i32 = vm.stack_peek(4)?;
  712. if is_arg_given(default_mask, 1) {
  713. let scalar: f32 = vm.stack_peek(1)?;
  714. vm.debug_str_append(&format!("{}", scalar));
  715. }
  716. if is_arg_given(default_mask, 2) {
  717. let (x, y): (f32, f32) = vm.stack_peek(2)?;
  718. vm.debug_str_append(&format!("({},{})", x, y));
  719. }
  720. if is_arg_given(default_mask, 3) {
  721. let (x, y): (f32, f32) = vm.stack_peek(3)?;
  722. if let Some(matrix) = context.matrix_stack.peek() {
  723. let (nx, ny) = matrix.transform_vec2(x, y);
  724. vm.debug_str_append(&format!("({},{})", nx, ny));
  725. }
  726. }
  727. Ok(None)
  728. }
  729. fn meta_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  730. Ok((
  731. // input arguments
  732. vec![
  733. (Keyword::LinearColourSpace, Var::Float(0.0)),
  734. ],
  735. // stack offset
  736. 0,
  737. ))
  738. }
  739. fn meta_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  740. let default_mask: i32 = vm.stack_peek(2)?;
  741. if is_arg_given(default_mask, 1) {
  742. let scalar: f32 = vm.stack_peek(1)?;
  743. context.output_linear_colour_space = scalar > 0.0;
  744. }
  745. Ok(None)
  746. }
  747. fn line_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  748. Ok((
  749. // input arguments
  750. vec![
  751. (Keyword::Width, Var::Float(4.0)),
  752. (Keyword::From, Var::V2D(10.0, 10.0)),
  753. (Keyword::To, Var::V2D(900.0, 900.0)),
  754. (Keyword::FromColour, Var::Colour(Default::default())),
  755. (Keyword::ToColour, Var::Colour(Default::default())),
  756. (Keyword::Colour, Var::Colour(Default::default())),
  757. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  758. (Keyword::BrushSubtype, Var::Float(1.0)),
  759. ],
  760. // stack offset
  761. 0,
  762. ))
  763. }
  764. fn line_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  765. let line_width: f32 = vm.stack_peek(1)?;
  766. let line_from: (f32, f32) = vm.stack_peek(2)?;
  767. let line_to: (f32, f32) = vm.stack_peek(3)?;
  768. let from_col: Colour = vm.stack_peek(4)?;
  769. let to_col: Colour = vm.stack_peek(5)?;
  770. let col: Colour = vm.stack_peek(6)?;
  771. let brush: Keyword = vm.stack_peek(7)?;
  772. let brush_subtype: usize = vm.stack_peek(8)?;
  773. let default_mask: i32 = vm.stack_peek(9)?;
  774. let brush_type = read_brush(brush);
  775. // if the from-colour and to-colour parameters are given
  776. if is_arg_given(default_mask, 4) && is_arg_given(default_mask, 5) {
  777. context.render_line(
  778. line_from,
  779. line_to,
  780. line_width,
  781. &from_col,
  782. &to_col,
  783. brush_type,
  784. brush_subtype,
  785. )?;
  786. } else {
  787. context.render_line(
  788. line_from,
  789. line_to,
  790. line_width,
  791. &col,
  792. &col,
  793. brush_type,
  794. brush_subtype,
  795. )?;
  796. };
  797. Ok(None)
  798. }
  799. fn rect_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  800. Ok((
  801. // input arguments
  802. vec![
  803. (Keyword::Width, Var::Float(4.0)),
  804. (Keyword::Height, Var::Float(10.0)),
  805. (Keyword::Position, Var::V2D(10.0, 10.0)),
  806. (Keyword::Colour, Var::Colour(Default::default())),
  807. ],
  808. // stack offset
  809. 0,
  810. ))
  811. }
  812. fn rect_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  813. let width: f32 = vm.stack_peek(1)?;
  814. let height: f32 = vm.stack_peek(2)?;
  815. let position: (f32, f32) = vm.stack_peek(3)?;
  816. let col: Colour = vm.stack_peek(4)?;
  817. context.render_rect(position, width, height, &col)?;
  818. Ok(None)
  819. }
  820. fn circle_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  821. Ok((
  822. // input arguments
  823. vec![
  824. (Keyword::Width, Var::Float(4.0)),
  825. (Keyword::Height, Var::Float(10.0)),
  826. (Keyword::Position, Var::V2D(10.0, 10.0)),
  827. (Keyword::Colour, Var::Colour(Default::default())),
  828. (Keyword::Tessellation, Var::Float(10.0)),
  829. (Keyword::Radius, Var::Float(10.0)),
  830. ],
  831. // stack offset
  832. 0,
  833. ))
  834. }
  835. fn circle_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  836. let width: f32 = vm.stack_peek(1)?;
  837. let height: f32 = vm.stack_peek(2)?;
  838. let position: (f32, f32) = vm.stack_peek(3)?;
  839. let col: Colour = vm.stack_peek(4)?;
  840. let tessellation: usize = vm.stack_peek(5)?;
  841. let radius: f32 = vm.stack_peek(6)?;
  842. let default_mask: i32 = vm.stack_peek(7)?;
  843. if is_arg_given(default_mask, 6) {
  844. // given a radius value
  845. context.render_circle(position, radius, radius, &col, tessellation)?;
  846. } else {
  847. // radius was not explicitly specified
  848. context.render_circle(position, width, height, &col, tessellation)?;
  849. }
  850. Ok(None)
  851. }
  852. fn circle_slice_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  853. Ok((
  854. // input arguments
  855. vec![
  856. (Keyword::Width, Var::Float(4.0)),
  857. (Keyword::Height, Var::Float(10.0)),
  858. (Keyword::Position, Var::V2D(10.0, 10.0)),
  859. (Keyword::Colour, Var::Colour(Default::default())),
  860. (Keyword::Tessellation, Var::Float(10.0)),
  861. (Keyword::Radius, Var::Float(10.0)),
  862. (Keyword::AngleStart, Var::Float(0.0)),
  863. (Keyword::AngleEnd, Var::Float(10.0)),
  864. (Keyword::InnerWidth, Var::Float(1.0)),
  865. (Keyword::InnerHeight, Var::Float(1.0)),
  866. ],
  867. // stack offset
  868. 0,
  869. ))
  870. }
  871. fn circle_slice_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  872. let mut width: f32 = vm.stack_peek(1)?;
  873. let mut height: f32 = vm.stack_peek(2)?;
  874. let position: (f32, f32) = vm.stack_peek(3)?;
  875. let col: Colour = vm.stack_peek(4)?;
  876. let tessellation: usize = vm.stack_peek(5)?;
  877. let radius: f32 = vm.stack_peek(6)?;
  878. let angle_start: f32 = vm.stack_peek(7)?;
  879. let angle_end: f32 = vm.stack_peek(8)?;
  880. let inner_width: f32 = vm.stack_peek(9)?;
  881. let inner_height: f32 = vm.stack_peek(10)?;
  882. let default_mask: i32 = vm.stack_peek(11)?;
  883. if is_arg_given(default_mask, 6) {
  884. // given a radius value
  885. width = radius;
  886. height = radius;
  887. }
  888. context.render_circle_slice(
  889. position,
  890. width,
  891. height,
  892. &col,
  893. tessellation,
  894. angle_start,
  895. angle_end,
  896. inner_width,
  897. inner_height,
  898. )?;
  899. Ok(None)
  900. }
  901. fn poly_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  902. Ok((
  903. // input arguments
  904. vec![
  905. (Keyword::Coords, Var::Bool(false)),
  906. (Keyword::Colours, Var::Bool(false)),
  907. ],
  908. // stack offset
  909. 0,
  910. ))
  911. }
  912. fn poly_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  913. let default_mask: i32 = vm.stack_peek(3)?;
  914. if !is_arg_given(default_mask, 1) || !is_arg_given(default_mask, 2) {
  915. error!("poly requires both coords and colours");
  916. return Err(Error::Native);
  917. }
  918. // code looks like this thanks to the borrow checker being anal
  919. let coords = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  920. let colours = stack_peek_vars(&vm.stack, vm.sp, 2)?;
  921. context.render_poly(coords, colours)?;
  922. Ok(None)
  923. }
  924. fn quadratic_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  925. Ok((
  926. // input arguments
  927. vec![
  928. (Keyword::LineWidth, Var::Float(4.0)),
  929. (Keyword::LineWidthStart, Var::Float(4.0)),
  930. (Keyword::LineWidthEnd, Var::Float(4.0)),
  931. (Keyword::LineWidthMapping, Var::Keyword(Keyword::Linear)),
  932. (Keyword::Coords, Var::Bool(false)),
  933. (Keyword::TStart, Var::Float(0.0)),
  934. (Keyword::TEnd, Var::Float(1.0)),
  935. (Keyword::Tessellation, Var::Float(10.0)),
  936. (Keyword::Colour, Var::Colour(Default::default())),
  937. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  938. (Keyword::BrushSubtype, Var::Float(1.0)),
  939. ],
  940. // stack offset
  941. 0,
  942. ))
  943. }
  944. fn quadratic_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  945. let line_width: f32 = vm.stack_peek(1)?;
  946. let mut line_width_start: f32 = vm.stack_peek(2)?;
  947. let mut line_width_end: f32 = vm.stack_peek(3)?;
  948. let line_width_mapping: Keyword = vm.stack_peek(4)?;
  949. let coords = stack_peek_vars(&vm.stack, vm.sp, 5)?;
  950. let t_start: f32 = vm.stack_peek(6)?;
  951. let t_end: f32 = vm.stack_peek(7)?;
  952. let tessellation: usize = vm.stack_peek(8)?;
  953. let col: Colour = vm.stack_peek(9)?;
  954. let brush: Keyword = vm.stack_peek(10)?;
  955. let brush_subtype: usize = vm.stack_peek(11)?;
  956. let default_mask: i32 = vm.stack_peek(12)?;
  957. if !is_arg_given(default_mask, 5) {
  958. error!("quadratic requires coords");
  959. return Err(Error::Native);
  960. }
  961. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  962. (x, y)
  963. } else {
  964. error!("coords 0 should be a Vec::V2D");
  965. return Err(Error::Native);
  966. };
  967. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  968. (x, y)
  969. } else {
  970. error!("coords 1 should be a Vec::V2D");
  971. return Err(Error::Native);
  972. };
  973. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  974. (x, y)
  975. } else {
  976. error!("coords 2 should be a Vec::V2D");
  977. return Err(Error::Native);
  978. };
  979. let brush_type = read_brush(brush);
  980. if let Some(mapping) = easing_from_keyword(line_width_mapping) {
  981. if is_arg_given(default_mask, 1) {
  982. // given a line width value
  983. line_width_start = line_width;
  984. line_width_end = line_width;
  985. }
  986. context.render_quadratic(
  987. &[x0, y0, x1, y1, x2, y2],
  988. line_width_start,
  989. line_width_end,
  990. mapping,
  991. t_start,
  992. t_end,
  993. &col,
  994. tessellation,
  995. brush_type,
  996. brush_subtype,
  997. )?;
  998. } else {
  999. error!("quadratic: invalid mapping");
  1000. return Err(Error::Native);
  1001. }
  1002. Ok(None)
  1003. }
  1004. fn bezier_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1005. Ok((
  1006. // input arguments
  1007. vec![
  1008. (Keyword::LineWidth, Var::Float(4.0)),
  1009. (Keyword::LineWidthStart, Var::Float(4.0)),
  1010. (Keyword::LineWidthEnd, Var::Float(4.0)),
  1011. (Keyword::LineWidthMapping, Var::Keyword(Keyword::Linear)),
  1012. (Keyword::Coords, Var::Bool(false)),
  1013. (Keyword::TStart, Var::Float(0.0)),
  1014. (Keyword::TEnd, Var::Float(1.0)),
  1015. (Keyword::Tessellation, Var::Float(10.0)),
  1016. (Keyword::Colour, Var::Colour(Default::default())),
  1017. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  1018. (Keyword::BrushSubtype, Var::Float(1.0)),
  1019. ],
  1020. // stack offset
  1021. 0,
  1022. ))
  1023. }
  1024. fn bezier_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1025. let line_width: f32 = vm.stack_peek(1)?;
  1026. let mut line_width_start: f32 = vm.stack_peek(2)?;
  1027. let mut line_width_end: f32 = vm.stack_peek(3)?;
  1028. let line_width_mapping: Keyword = vm.stack_peek(4)?;
  1029. let coords = stack_peek_vars(&vm.stack, vm.sp, 5)?;
  1030. let t_start: f32 = vm.stack_peek(6)?;
  1031. let t_end: f32 = vm.stack_peek(7)?;
  1032. let tessellation: usize = vm.stack_peek(8)?;
  1033. let col: Colour = vm.stack_peek(9)?;
  1034. let brush: Keyword = vm.stack_peek(10)?;
  1035. let brush_subtype: usize = vm.stack_peek(11)?;
  1036. let default_mask: i32 = vm.stack_peek(12)?;
  1037. if !is_arg_given(default_mask, 5) {
  1038. error!("bezier requires coords");
  1039. return Err(Error::Native);
  1040. }
  1041. let brush_type = read_brush(brush);
  1042. if let Some(mapping) = easing_from_keyword(line_width_mapping) {
  1043. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  1044. (x, y)
  1045. } else {
  1046. error!("coords 0 should be a Vec::V2D");
  1047. return Err(Error::Native);
  1048. };
  1049. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  1050. (x, y)
  1051. } else {
  1052. error!("coords 1 should be a Vec::V2D");
  1053. return Err(Error::Native);
  1054. };
  1055. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  1056. (x, y)
  1057. } else {
  1058. error!("coords 2 should be a Vec::V2D");
  1059. return Err(Error::Native);
  1060. };
  1061. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  1062. (x, y)
  1063. } else {
  1064. error!("coords 3 should be a Vec::V2D");
  1065. return Err(Error::Native);
  1066. };
  1067. if is_arg_given(default_mask, 1) {
  1068. // given a line width value
  1069. line_width_start = line_width;
  1070. line_width_end = line_width;
  1071. }
  1072. context.render_bezier(
  1073. &[x0, y0, x1, y1, x2, y2, x3, y3],
  1074. line_width_start,
  1075. line_width_end,
  1076. mapping,
  1077. t_start,
  1078. t_end,
  1079. &col,
  1080. tessellation,
  1081. brush_type,
  1082. brush_subtype,
  1083. )?;
  1084. } else {
  1085. error!("bezier: invalid mapping");
  1086. return Err(Error::Native);
  1087. }
  1088. Ok(None)
  1089. }
  1090. fn bezier_bulging_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1091. Ok((
  1092. // input arguments
  1093. vec![
  1094. (Keyword::LineWidth, Var::Float(4.0)),
  1095. (Keyword::Coords, Var::Bool(false)),
  1096. (Keyword::TStart, Var::Float(0.0)),
  1097. (Keyword::TEnd, Var::Float(1.0)),
  1098. (Keyword::Tessellation, Var::Float(10.0)),
  1099. (Keyword::Colour, Var::Colour(Default::default())),
  1100. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  1101. (Keyword::BrushSubtype, Var::Float(1.0)),
  1102. ],
  1103. // stack offset
  1104. 0,
  1105. ))
  1106. }
  1107. fn bezier_bulging_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1108. let line_width: f32 = vm.stack_peek(1)?;
  1109. let coords = stack_peek_vars(&vm.stack, vm.sp, 2)?;
  1110. let t_start: f32 = vm.stack_peek(3)?;
  1111. let t_end: f32 = vm.stack_peek(4)?;
  1112. let tessellation: usize = vm.stack_peek(5)?;
  1113. let col: Colour = vm.stack_peek(6)?;
  1114. let brush: Keyword = vm.stack_peek(7)?;
  1115. let brush_subtype: usize = vm.stack_peek(8)?;
  1116. let default_mask: i32 = vm.stack_peek(9)?;
  1117. if !is_arg_given(default_mask, 2) {
  1118. error!("bezier_bulging requires coords");
  1119. return Err(Error::Native);
  1120. }
  1121. let brush_type = read_brush(brush);
  1122. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  1123. (x, y)
  1124. } else {
  1125. error!("coords 0 should be a Vec::V2D");
  1126. return Err(Error::Native);
  1127. };
  1128. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  1129. (x, y)
  1130. } else {
  1131. error!("coords 1 should be a Vec::V2D");
  1132. return Err(Error::Native);
  1133. };
  1134. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  1135. (x, y)
  1136. } else {
  1137. error!("coords 2 should be a Vec::V2D");
  1138. return Err(Error::Native);
  1139. };
  1140. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  1141. (x, y)
  1142. } else {
  1143. error!("coords 3 should be a Vec::V2D");
  1144. return Err(Error::Native);
  1145. };
  1146. context.render_bezier_bulging(
  1147. &[x0, y0, x1, y1, x2, y2, x3, y3],
  1148. line_width,
  1149. t_start,
  1150. t_end,
  1151. &col,
  1152. tessellation,
  1153. brush_type,
  1154. brush_subtype,
  1155. )?;
  1156. Ok(None)
  1157. }
  1158. fn stroked_bezier_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1159. Ok((
  1160. // input arguments
  1161. vec![
  1162. (Keyword::Tessellation, Var::Float(15.0)),
  1163. (Keyword::Coords, Var::Bool(false)),
  1164. (Keyword::StrokeTessellation, Var::Float(10.0)),
  1165. (Keyword::StrokeNoise, Var::Float(25.0)),
  1166. (Keyword::StrokeLineWidthStart, Var::Float(1.0)),
  1167. (Keyword::StrokeLineWidthEnd, Var::Float(1.0)),
  1168. (Keyword::Colour, Var::Colour(Default::default())),
  1169. (Keyword::ColourVolatility, Var::Float(0.0)),
  1170. (Keyword::Seed, Var::Float(0.0)),
  1171. (Keyword::LineWidthMapping, Var::Keyword(Keyword::Linear)),
  1172. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  1173. (Keyword::BrushSubtype, Var::Float(1.0)),
  1174. ],
  1175. // stack offset
  1176. 0,
  1177. ))
  1178. }
  1179. fn stroked_bezier_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1180. let tessellation: usize = vm.stack_peek(1)?;
  1181. let coords = stack_peek_vars(&vm.stack, vm.sp, 2)?;
  1182. let stroke_tessellation: usize = vm.stack_peek(3)?;
  1183. let stroke_noise: f32 = vm.stack_peek(4)?;
  1184. let stroke_line_width_start: f32 = vm.stack_peek(5)?;
  1185. let stroke_line_width_end: f32 = vm.stack_peek(6)?;
  1186. let col: Colour = vm.stack_peek(7)?;
  1187. let col_volatility: f32 = vm.stack_peek(8)?;
  1188. let seed: f32 = vm.stack_peek(9)?;
  1189. let line_width_mapping: Keyword = vm.stack_peek(10)?;
  1190. let brush: Keyword = vm.stack_peek(11)?;
  1191. let brush_subtype: usize = vm.stack_peek(12)?;
  1192. let default_mask: i32 = vm.stack_peek(13)?;
  1193. if !is_arg_given(default_mask, 2) {
  1194. error!("stroked bezier requires coords");
  1195. return Err(Error::Native);
  1196. }
  1197. let brush_type = read_brush(brush);
  1198. if let Some(mapping) = easing_from_keyword(line_width_mapping) {
  1199. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  1200. (x, y)
  1201. } else {
  1202. error!("coords 0 should be a Vec::V2D");
  1203. return Err(Error::Native);
  1204. };
  1205. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  1206. (x, y)
  1207. } else {
  1208. error!("coords 1 should be a Vec::V2D");
  1209. return Err(Error::Native);
  1210. };
  1211. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  1212. (x, y)
  1213. } else {
  1214. error!("coords 2 should be a Vec::V2D");
  1215. return Err(Error::Native);
  1216. };
  1217. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  1218. (x, y)
  1219. } else {
  1220. error!("coords 3 should be a Vec::V2D");
  1221. return Err(Error::Native);
  1222. };
  1223. context.render_stroked_bezier(
  1224. tessellation,
  1225. &[x0, y0, x1, y1, x2, y2, x3, y3],
  1226. stroke_tessellation,
  1227. stroke_noise,
  1228. stroke_line_width_start,
  1229. stroke_line_width_end,
  1230. &col,
  1231. col_volatility,
  1232. seed,
  1233. mapping,
  1234. brush_type,
  1235. brush_subtype,
  1236. )?
  1237. } else {
  1238. error!("stroked bezier: invalid mapping");
  1239. return Err(Error::Native);
  1240. }
  1241. Ok(None)
  1242. }
  1243. fn stroked_bezier_rect_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1244. Ok((
  1245. // input arguments
  1246. vec![
  1247. (Keyword::Position, Var::V2D(100.0, 100.0)),
  1248. (Keyword::Width, Var::Float(80.0)),
  1249. (Keyword::Height, Var::Float(600.0)),
  1250. (Keyword::Volatility, Var::Float(30.0)),
  1251. (Keyword::Overlap, Var::Float(0.0)),
  1252. (Keyword::Iterations, Var::Float(10.0)),
  1253. (Keyword::Seed, Var::Float(0.0)),
  1254. (Keyword::Tessellation, Var::Float(15.0)),
  1255. (Keyword::StrokeTessellation, Var::Float(10.0)),
  1256. (Keyword::StrokeNoise, Var::Float(25.0)),
  1257. (Keyword::Colour, Var::Colour(Default::default())),
  1258. (Keyword::ColourVolatility, Var::Float(0.0)),
  1259. (Keyword::Brush, Var::Keyword(Keyword::BrushFlat)),
  1260. (Keyword::BrushSubtype, Var::Float(1.0)),
  1261. ],
  1262. // stack offset
  1263. 0,
  1264. ))
  1265. }
  1266. fn stroked_bezier_rect_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1267. let position: (f32, f32) = vm.stack_peek(1)?;
  1268. let width: f32 = vm.stack_peek(2)?;
  1269. let height: f32 = vm.stack_peek(3)?;
  1270. let volatility: f32 = vm.stack_peek(4)?;
  1271. let overlap: f32 = vm.stack_peek(5)?;
  1272. let iterations: f32 = vm.stack_peek(6)?;
  1273. let seed: f32 = vm.stack_peek(7)?;
  1274. let tessellation: usize = vm.stack_peek(8)?;
  1275. let stroke_tessellation: usize = vm.stack_peek(9)?;
  1276. let stroke_noise: f32 = vm.stack_peek(10)?;
  1277. let col: Colour = vm.stack_peek(11)?;
  1278. let col_volatility: f32 = vm.stack_peek(12)?;
  1279. let brush: Keyword = vm.stack_peek(13)?;
  1280. let brush_subtype: usize = vm.stack_peek(14)?;
  1281. let brush_type = read_brush(brush);
  1282. context.render_stroked_bezier_rect(
  1283. position,
  1284. width,
  1285. height,
  1286. volatility,
  1287. overlap,
  1288. iterations,
  1289. seed as i32,
  1290. tessellation,
  1291. stroke_tessellation,
  1292. stroke_noise,
  1293. &col,
  1294. col_volatility,
  1295. brush_type,
  1296. brush_subtype,
  1297. )?;
  1298. Ok(None)
  1299. }
  1300. fn matrix_push_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1301. Ok((
  1302. // input arguments
  1303. vec![],
  1304. // stack offset
  1305. 0,
  1306. ))
  1307. }
  1308. fn matrix_push_execute(_vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1309. context.matrix_stack.push();
  1310. Ok(None)
  1311. }
  1312. fn matrix_pop_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1313. Ok((
  1314. // input arguments
  1315. vec![],
  1316. // stack offset
  1317. 0,
  1318. ))
  1319. }
  1320. fn matrix_pop_execute(_vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1321. context.matrix_stack.pop();
  1322. Ok(None)
  1323. }
  1324. fn translate_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1325. Ok((
  1326. // input arguments
  1327. vec![(Keyword::Vector, Var::V2D(0.0, 0.0))],
  1328. // stack offset
  1329. 0,
  1330. ))
  1331. }
  1332. fn translate_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1333. let (x, y): (f32, f32) = vm.stack_peek(1)?;
  1334. context.matrix_stack.translate(x, y);
  1335. Ok(None)
  1336. }
  1337. fn rotate_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1338. Ok((
  1339. // input arguments
  1340. vec![(Keyword::Angle, Var::Float(0.0))],
  1341. // stack offset
  1342. 0,
  1343. ))
  1344. }
  1345. fn rotate_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1346. let angle: f32 = vm.stack_peek(1)?;
  1347. context.matrix_stack.rotate(mathutil::deg_to_rad(angle));
  1348. Ok(None)
  1349. }
  1350. fn scale_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1351. Ok((
  1352. // input arguments
  1353. vec![
  1354. (Keyword::Vector, Var::V2D(1.0, 1.0)),
  1355. (Keyword::Scalar, Var::Float(1.0)),
  1356. ],
  1357. // stack offset
  1358. 0,
  1359. ))
  1360. }
  1361. fn scale_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  1362. let (x, y): (f32, f32) = vm.stack_peek(1)?;
  1363. let scalar: f32 = vm.stack_peek(2)?;
  1364. let default_mask: i32 = vm.stack_peek(3)?;
  1365. if is_arg_given(default_mask, 2) {
  1366. // scalar was specified in the script
  1367. context.matrix_stack.scale(scalar, scalar);
  1368. } else {
  1369. context.matrix_stack.scale(x, y);
  1370. }
  1371. Ok(None)
  1372. }
  1373. fn col_convert_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1374. Ok((
  1375. // input arguments
  1376. vec![
  1377. (Keyword::Format, Var::Bool(false)),
  1378. (Keyword::Colour, Var::Colour(Default::default())),
  1379. ],
  1380. // stack offset
  1381. 1,
  1382. ))
  1383. }
  1384. fn col_convert_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1385. let format: Keyword = vm.stack_peek(1)?;
  1386. let col: Colour = vm.stack_peek(2)?;
  1387. let default_mask: i32 = vm.stack_peek(3)?;
  1388. if !is_arg_given(default_mask, 1) {
  1389. error!("col/convert requires format argument");
  1390. Err(Error::Native)
  1391. } else if let Some(format) = ColourFormat::from_keyword(format) {
  1392. let col = col.convert(format)?;
  1393. Ok(Some(Var::Colour(col)))
  1394. } else {
  1395. error!("col/convert");
  1396. Err(Error::Native)
  1397. }
  1398. }
  1399. fn col_rgb_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1400. Ok((
  1401. // input arguments
  1402. vec![
  1403. (Keyword::R, Var::Float(0.0)),
  1404. (Keyword::G, Var::Float(0.0)),
  1405. (Keyword::B, Var::Float(0.0)),
  1406. (Keyword::Alpha, Var::Float(1.0)),
  1407. ],
  1408. // stack offset
  1409. 1,
  1410. ))
  1411. }
  1412. fn col_rgb_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1413. let r: f32 = vm.stack_peek(1)?;
  1414. let g: f32 = vm.stack_peek(2)?;
  1415. let b: f32 = vm.stack_peek(3)?;
  1416. let alpha: f32 = vm.stack_peek(4)?;
  1417. Ok(Some(Var::Colour(Colour::new(
  1418. ColourFormat::Rgb,
  1419. r,
  1420. g,
  1421. b,
  1422. alpha,
  1423. ))))
  1424. }
  1425. fn col_hsl_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1426. Ok((
  1427. // input arguments
  1428. vec![
  1429. (Keyword::H, Var::Float(0.0)),
  1430. (Keyword::S, Var::Float(0.0)),
  1431. (Keyword::L, Var::Float(0.0)),
  1432. (Keyword::Alpha, Var::Float(1.0)),
  1433. ],
  1434. // stack offset
  1435. 1,
  1436. ))
  1437. }
  1438. fn col_hsl_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1439. let h: f32 = vm.stack_peek(1)?;
  1440. let s: f32 = vm.stack_peek(2)?;
  1441. let l: f32 = vm.stack_peek(3)?;
  1442. let alpha: f32 = vm.stack_peek(4)?;
  1443. Ok(Some(Var::Colour(Colour::new(
  1444. ColourFormat::Hsl,
  1445. h,
  1446. s,
  1447. l,
  1448. alpha,
  1449. ))))
  1450. }
  1451. fn col_hsluv_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1452. Ok((
  1453. // input arguments
  1454. vec![
  1455. (Keyword::H, Var::Float(0.0)),
  1456. (Keyword::S, Var::Float(0.0)),
  1457. (Keyword::L, Var::Float(0.0)),
  1458. (Keyword::Alpha, Var::Float(1.0)),
  1459. ],
  1460. // stack offset
  1461. 1,
  1462. ))
  1463. }
  1464. fn col_hsluv_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1465. let h: f32 = vm.stack_peek(1)?;
  1466. let s: f32 = vm.stack_peek(2)?;
  1467. let l: f32 = vm.stack_peek(3)?;
  1468. let alpha: f32 = vm.stack_peek(4)?;
  1469. let colour = Colour::new(ColourFormat::Hsluv, h, s, l, alpha);
  1470. // error!("col/hsluv: {:?}", &colour);
  1471. Ok(Some(Var::Colour(colour)))
  1472. }
  1473. fn col_hsv_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1474. Ok((
  1475. // input arguments
  1476. vec![
  1477. (Keyword::H, Var::Float(0.0)),
  1478. (Keyword::S, Var::Float(0.0)),
  1479. (Keyword::V, Var::Float(0.0)),
  1480. (Keyword::Alpha, Var::Float(1.0)),
  1481. ],
  1482. // stack offset
  1483. 1,
  1484. ))
  1485. }
  1486. fn col_hsv_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1487. let h: f32 = vm.stack_peek(1)?;
  1488. let s: f32 = vm.stack_peek(2)?;
  1489. let v: f32 = vm.stack_peek(3)?;
  1490. let alpha: f32 = vm.stack_peek(4)?;
  1491. Ok(Some(Var::Colour(Colour::new(
  1492. ColourFormat::Hsv,
  1493. h,
  1494. s,
  1495. v,
  1496. alpha,
  1497. ))))
  1498. }
  1499. fn col_lab_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1500. Ok((
  1501. // input arguments
  1502. vec![
  1503. (Keyword::L, Var::Float(0.0)),
  1504. (Keyword::A, Var::Float(0.0)),
  1505. (Keyword::B, Var::Float(0.0)),
  1506. (Keyword::Alpha, Var::Float(1.0)),
  1507. ],
  1508. // stack offset
  1509. 1,
  1510. ))
  1511. }
  1512. fn col_lab_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1513. let l: f32 = vm.stack_peek(1)?;
  1514. let a: f32 = vm.stack_peek(2)?;
  1515. let b: f32 = vm.stack_peek(3)?;
  1516. let alpha: f32 = vm.stack_peek(4)?;
  1517. Ok(Some(Var::Colour(Colour::new(
  1518. ColourFormat::Lab,
  1519. l,
  1520. a,
  1521. b,
  1522. alpha,
  1523. ))))
  1524. }
  1525. fn col_complementary_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1526. Ok((
  1527. // input arguments
  1528. vec![(Keyword::Colour, Var::Colour(Default::default()))],
  1529. // stack offset
  1530. 1,
  1531. ))
  1532. }
  1533. fn col_complementary_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1534. let col: Colour = vm.stack_peek(1)?;
  1535. Ok(Some(Var::Colour(col.complementary()?)))
  1536. }
  1537. fn col_split_complementary_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1538. Ok((
  1539. // input arguments
  1540. vec![(Keyword::Colour, Var::Colour(Default::default()))],
  1541. // stack offset
  1542. 1,
  1543. ))
  1544. }
  1545. fn col_split_complementary_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1546. let col: Colour = vm.stack_peek(1)?;
  1547. let (col1, col2) = col.split_complementary()?;
  1548. Ok(Some(Var::Vector(vec![
  1549. Var::Colour(col1),
  1550. Var::Colour(col2),
  1551. ])))
  1552. }
  1553. fn col_analagous_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1554. Ok((
  1555. // input arguments
  1556. vec![(Keyword::Colour, Var::Colour(Default::default()))],
  1557. // stack offset
  1558. 1,
  1559. ))
  1560. }
  1561. fn col_analagous_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1562. let col: Colour = vm.stack_peek(1)?;
  1563. let (col1, col2) = col.analagous()?;
  1564. Ok(Some(Var::Vector(vec![
  1565. Var::Colour(col1),
  1566. Var::Colour(col2),
  1567. ])))
  1568. }
  1569. fn col_triad_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1570. Ok((
  1571. // input arguments
  1572. vec![(Keyword::Colour, Var::Colour(Default::default()))],
  1573. // stack offset
  1574. 1,
  1575. ))
  1576. }
  1577. fn col_triad_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1578. let col: Colour = vm.stack_peek(1)?;
  1579. let (col1, col2) = col.triad()?;
  1580. Ok(Some(Var::Vector(vec![
  1581. Var::Colour(col1),
  1582. Var::Colour(col2),
  1583. ])))
  1584. }
  1585. fn common_colour_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1586. Ok((
  1587. // input arguments
  1588. vec![
  1589. (Keyword::Colour, Var::Colour(Default::default())),
  1590. (Keyword::Value, Var::Float(0.0)),
  1591. ],
  1592. // stack offset
  1593. 1,
  1594. ))
  1595. }
  1596. fn common_colour_only_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1597. Ok((
  1598. // input arguments
  1599. vec![(Keyword::Colour, Var::Colour(Default::default()))],
  1600. // stack offset
  1601. 1,
  1602. ))
  1603. }
  1604. fn col_darken_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1605. let col: Colour = vm.stack_peek(1)?;
  1606. let value: f32 = vm.stack_peek(2)?;
  1607. Ok(Some(Var::Colour(col.darken(value)?)))
  1608. }
  1609. fn col_lighten_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1610. let col: Colour = vm.stack_peek(1)?;
  1611. let value: f32 = vm.stack_peek(2)?;
  1612. Ok(Some(Var::Colour(col.lighten(value)?)))
  1613. }
  1614. fn col_set_elem_execute(vm: &mut Vm, idx: usize) -> Result<Option<Var>> {
  1615. let col: Colour = vm.stack_peek(1)?;
  1616. let value: f32 = vm.stack_peek(2)?;
  1617. let res = match idx {
  1618. 0 => Colour::new(col.format, value, col.e1, col.e2, col.e3),
  1619. 1 => Colour::new(col.format, col.e0, value, col.e2, col.e3),
  1620. 2 => Colour::new(col.format, col.e0, col.e1, value, col.e3),
  1621. 3 => Colour::new(col.format, col.e0, col.e1, col.e2, value),
  1622. _ => {
  1623. error!("col_set_elem_execute::idx out of range");
  1624. return Err(Error::Native);
  1625. }
  1626. };
  1627. Ok(Some(Var::Colour(res)))
  1628. }
  1629. fn col_get_elem_execute(vm: &mut Vm, idx: usize) -> Result<Option<Var>> {
  1630. let col: Colour = vm.stack_peek(1)?;
  1631. let res = match idx {
  1632. 0 => col.e0,
  1633. 1 => col.e1,
  1634. 2 => col.e2,
  1635. 3 => col.e3,
  1636. _ => {
  1637. error!("col_get_elem_execute::idx out of range");
  1638. return Err(Error::Native);
  1639. }
  1640. };
  1641. Ok(Some(Var::Float(res)))
  1642. }
  1643. fn col_build_procedural_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1644. Ok((
  1645. // input arguments
  1646. vec![
  1647. (Keyword::Preset, Var::Keyword(Keyword::Robocop)),
  1648. (Keyword::Alpha, Var::Float(1.0)),
  1649. (Keyword::A, Var::Float(0.0)),
  1650. (Keyword::B, Var::Float(0.0)),
  1651. (Keyword::C, Var::Float(0.0)),
  1652. (Keyword::D, Var::Float(0.0)),
  1653. ],
  1654. // stack offset
  1655. 1,
  1656. ))
  1657. }
  1658. fn to_f32_3(vecs: &[Var]) -> Result<[f32; 3]> {
  1659. if let Var::Float(a) = vecs[0] {
  1660. if let Var::Float(b) = vecs[1] {
  1661. if let Var::Float(c) = vecs[2] {
  1662. return Ok([a, b, c]);
  1663. }
  1664. }
  1665. }
  1666. error!("to_f32_3");
  1667. Err(Error::Native)
  1668. }
  1669. fn col_build_procedural_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1670. let default_mask: i32 = vm.stack_peek(7)?;
  1671. let alpha: f32 = vm.stack_peek(2)?;
  1672. let (a, b, c, d) = if is_arg_given(default_mask, 1) {
  1673. // preset given
  1674. let preset_kw: Keyword = vm.stack_peek(1)?;
  1675. if let Some(preset) = ColourPreset::from_keyword(preset_kw) {
  1676. preset.get_preset()
  1677. } else {
  1678. error!("col_build_procedural_execute");
  1679. return Err(Error::Native);
  1680. }
  1681. } else if is_arg_given(default_mask, 3)
  1682. && is_arg_given(default_mask, 4)
  1683. && is_arg_given(default_mask, 5)
  1684. && is_arg_given(default_mask, 6)
  1685. {
  1686. (
  1687. to_f32_3(stack_peek_vars(&vm.stack, vm.sp, 3)?)?,
  1688. to_f32_3(stack_peek_vars(&vm.stack, vm.sp, 4)?)?,
  1689. to_f32_3(stack_peek_vars(&vm.stack, vm.sp, 5)?)?,
  1690. to_f32_3(stack_peek_vars(&vm.stack, vm.sp, 6)?)?,
  1691. )
  1692. } else {
  1693. error!("col_build_procedural_execute");
  1694. return Err(Error::Native);
  1695. };
  1696. Ok(Some(Var::ProcColourState(ProcColourStateStruct {
  1697. a,
  1698. b,
  1699. c,
  1700. d,
  1701. alpha,
  1702. })))
  1703. }
  1704. fn col_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1705. Ok((
  1706. // input arguments
  1707. vec![
  1708. (Keyword::From, Var::Bool(false)),
  1709. (Keyword::T, Var::Float(1.0)),
  1710. ],
  1711. // stack offset
  1712. 1,
  1713. ))
  1714. }
  1715. fn col_value_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1716. let default_mask: i32 = vm.stack_peek(3)?;
  1717. if !is_arg_given(default_mask, 1) {
  1718. error!("col_value_execute requires from parameter");
  1719. return Err(Error::Native);
  1720. }
  1721. let from = stack_peek_proc_colour_state_struct(&vm.stack, vm.sp, 1)?;
  1722. let t: f32 = vm.stack_peek(2)?;
  1723. let res = from.colour(t);
  1724. Ok(Some(Var::Colour(res)))
  1725. }
  1726. fn math_distance_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1727. Ok((
  1728. // input arguments
  1729. vec![
  1730. (Keyword::Vec1, Var::V2D(0.0, 0.0)),
  1731. (Keyword::Vec2, Var::V2D(0.0, 0.0)),
  1732. ],
  1733. // stack offset
  1734. 1,
  1735. ))
  1736. }
  1737. fn math_distance_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1738. let (x1, y1): (f32, f32) = vm.stack_peek(1)?;
  1739. let (x2, y2): (f32, f32) = vm.stack_peek(2)?;
  1740. let distance = mathutil::distance_v2(x1, y1, x2, y2);
  1741. Ok(Some(Var::Float(distance)))
  1742. }
  1743. fn math_normal_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1744. Ok((
  1745. // input arguments
  1746. vec![
  1747. (Keyword::Vec1, Var::V2D(0.0, 0.0)),
  1748. (Keyword::Vec2, Var::V2D(0.0, 0.0)),
  1749. ],
  1750. // stack offset
  1751. 1,
  1752. ))
  1753. }
  1754. fn math_normal_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1755. let (x1, y1): (f32, f32) = vm.stack_peek(1)?;
  1756. let (x2, y2): (f32, f32) = vm.stack_peek(2)?;
  1757. let distance = mathutil::normal(x1, y1, x2, y2);
  1758. Ok(Some(Var::V2D(distance.0, distance.1)))
  1759. }
  1760. fn math_clamp_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1761. Ok((
  1762. // input arguments
  1763. vec![
  1764. (Keyword::Value, Var::Float(0.0)),
  1765. (Keyword::Min, Var::Float(0.0)),
  1766. (Keyword::Max, Var::Float(0.0)),
  1767. ],
  1768. // stack offset
  1769. 1,
  1770. ))
  1771. }
  1772. fn math_clamp_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1773. // todo: try and move functions like this into ones that initially
  1774. // create and return a function that takes a single argument.
  1775. // e.g.
  1776. // (define my-clamp (math/clamp-fn min: 0.0 max: 42.0))
  1777. // (my-clamp val: 22)
  1778. //
  1779. // then optimize for single argument functions as these will be much faster to
  1780. // parse
  1781. //
  1782. let value: f32 = vm.stack_peek(1)?;
  1783. let min: f32 = vm.stack_peek(2)?;
  1784. let max: f32 = vm.stack_peek(3)?;
  1785. let clamped = mathutil::clamp(value, min, max);
  1786. Ok(Some(Var::Float(clamped)))
  1787. }
  1788. fn math_radians_degrees_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1789. Ok((
  1790. // input arguments
  1791. vec![(Keyword::Angle, Var::Float(0.0))],
  1792. // stack offset
  1793. 1,
  1794. ))
  1795. }
  1796. fn math_radians_degrees_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1797. let rad_angle: f32 = vm.stack_peek(1)?;
  1798. let deg_angle = mathutil::rad_to_deg(rad_angle);
  1799. Ok(Some(Var::Float(deg_angle)))
  1800. }
  1801. fn math_cos_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1802. Ok((
  1803. // input arguments
  1804. vec![(Keyword::Angle, Var::Float(0.0))],
  1805. // stack offset
  1806. 1,
  1807. ))
  1808. }
  1809. fn math_cos_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1810. let angle: f32 = vm.stack_peek(1)?;
  1811. let c = angle.cos();
  1812. Ok(Some(Var::Float(c)))
  1813. }
  1814. fn math_sin_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1815. Ok((
  1816. // input arguments
  1817. vec![(Keyword::Angle, Var::Float(0.0))],
  1818. // stack offset
  1819. 1,
  1820. ))
  1821. }
  1822. fn math_sin_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1823. let angle: f32 = vm.stack_peek(1)?;
  1824. let s = angle.sin();
  1825. Ok(Some(Var::Float(s)))
  1826. }
  1827. fn prng_build_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1828. Ok((
  1829. // input arguments
  1830. vec![
  1831. (Keyword::Seed, Var::Float(1.0)),
  1832. (Keyword::Min, Var::Float(0.0)),
  1833. (Keyword::Max, Var::Float(1.0)),
  1834. ],
  1835. // stack offset
  1836. 1,
  1837. ))
  1838. }
  1839. fn prng_build_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1840. let seed: f32 = vm.stack_peek(1)?;
  1841. let min: f32 = vm.stack_peek(2)?;
  1842. let max: f32 = vm.stack_peek(3)?;
  1843. let prng_state_struct = prng::PrngStateStruct::new(seed as i32, min, max);
  1844. Ok(Some(Var::PrngState(Rc::new(RefCell::new(
  1845. prng_state_struct,
  1846. )))))
  1847. }
  1848. fn prng_values_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1849. Ok((
  1850. // input arguments
  1851. vec![
  1852. (Keyword::From, Var::Bool(false)),
  1853. (Keyword::Num, Var::Float(1.0)),
  1854. ],
  1855. // stack offset
  1856. 1,
  1857. ))
  1858. }
  1859. fn prng_values_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1860. let default_mask: i32 = vm.stack_peek(3)?;
  1861. if !is_arg_given(default_mask, 1) {
  1862. error!("prng/values requires a from parameter");
  1863. return Err(Error::Native);
  1864. }
  1865. let mut ref_mut_prng_state = ref_mut_prng_state_struct(&vm.stack, vm.sp, 1)?;
  1866. let num: f32 = vm.stack_peek(2)?;
  1867. let num = num as i32;
  1868. let mut vs: Vec<Var> = Vec::new();
  1869. for _ in 0..num {
  1870. let f = ref_mut_prng_state.next_f32_defined_range();
  1871. vs.push(Var::Float(f))
  1872. }
  1873. Ok(Some(Var::Vector(vs)))
  1874. }
  1875. fn prng_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1876. Ok((
  1877. // input arguments
  1878. vec![(Keyword::From, Var::Bool(false))],
  1879. // stack offset
  1880. 1,
  1881. ))
  1882. }
  1883. fn prng_value_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1884. let default_mask: i32 = vm.stack_peek(2)?;
  1885. if !is_arg_given(default_mask, 1) {
  1886. error!("prng/value requires a from parameter");
  1887. return Err(Error::Native);
  1888. }
  1889. let mut ref_mut_prng_state = ref_mut_prng_state_struct(&vm.stack, vm.sp, 1)?;
  1890. let res = ref_mut_prng_state.next_f32_defined_range();
  1891. Ok(Some(Var::Float(res)))
  1892. }
  1893. fn prng_perlin_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1894. Ok((
  1895. // input arguments
  1896. vec![
  1897. (Keyword::X, Var::Float(1.0)),
  1898. (Keyword::Y, Var::Float(1.0)),
  1899. (Keyword::Z, Var::Float(1.0)),
  1900. ],
  1901. // stack offset
  1902. 1,
  1903. ))
  1904. }
  1905. fn prng_perlin_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1906. let x: f32 = vm.stack_peek(1)?;
  1907. let y: f32 = vm.stack_peek(2)?;
  1908. let z: f32 = vm.stack_peek(3)?;
  1909. let res = prng::perlin(x, y, z);
  1910. Ok(Some(Var::Float(res)))
  1911. }
  1912. fn interp_build_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1913. Ok((
  1914. // input arguments
  1915. vec![
  1916. (Keyword::From, Var::V2D(0.0, 1.0)),
  1917. (Keyword::To, Var::V2D(0.0, 100.0)),
  1918. (Keyword::Clamping, Var::Keyword(Keyword::False)),
  1919. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  1920. ],
  1921. // stack offset
  1922. 1,
  1923. ))
  1924. }
  1925. fn interp_build_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1926. let from: (f32, f32) = vm.stack_peek(1)?;
  1927. let to: (f32, f32) = vm.stack_peek(2)?;
  1928. let clamping: Keyword = vm.stack_peek(3)?;
  1929. let mapping: Keyword = vm.stack_peek(4)?;
  1930. let clamping = clamping == Keyword::True;
  1931. if let Some(mapping) = easing_from_keyword(mapping) {
  1932. let from_m = mathutil::mc_m(from.0, 0.0, from.1, 1.0);
  1933. let from_c = mathutil::mc_c(from.0, 0.0, from_m);
  1934. let to_m = mathutil::mc_m(0.0, to.0, 1.0, to.1);
  1935. let to_c = mathutil::mc_c(0.0, to.0, to_m);
  1936. Ok(Some(Var::InterpState(interp::InterpStateStruct {
  1937. from_m,
  1938. to_m,
  1939. from_c,
  1940. to_c,
  1941. to,
  1942. clamping,
  1943. mapping,
  1944. })))
  1945. } else {
  1946. error!("interp_build_execute");
  1947. Err(Error::Native)
  1948. }
  1949. }
  1950. fn interp_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1951. Ok((
  1952. // input arguments
  1953. vec![
  1954. (Keyword::From, Var::Bool(false)),
  1955. (Keyword::T, Var::Float(0.0)),
  1956. ],
  1957. // stack offset
  1958. 1,
  1959. ))
  1960. }
  1961. fn interp_value_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1962. let default_mask: i32 = vm.stack_peek(3)?;
  1963. if !is_arg_given(default_mask, 1) {
  1964. error!("interp/value requires a from parameter");
  1965. return Err(Error::Native);
  1966. }
  1967. let interp_state = stack_peek_interp_state_struct(&vm.stack, vm.sp, 1)?;
  1968. let t: f32 = vm.stack_peek(2)?;
  1969. let res = interp_state.value(t);
  1970. // error!("interp/value = {:?} t: {} interp_state: {:?}", res, t, &interp_state);
  1971. Ok(Some(Var::Float(res)))
  1972. }
  1973. fn interp_cos_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1974. Ok((
  1975. // input arguments
  1976. vec![
  1977. (Keyword::Amplitude, Var::Float(1.0)),
  1978. (Keyword::Frequency, Var::Float(1.0)),
  1979. (Keyword::T, Var::Float(1.0)),
  1980. ],
  1981. // stack offset
  1982. 1,
  1983. ))
  1984. }
  1985. fn interp_cos_execute(vm: &mut Vm) -> Result<Option<Var>> {
  1986. let amplitude: f32 = vm.stack_peek(1)?;
  1987. let frequency: f32 = vm.stack_peek(2)?;
  1988. let t: f32 = vm.stack_peek(3)?;
  1989. let res = interp::cos(amplitude, frequency, t);
  1990. Ok(Some(Var::Float(res)))
  1991. }
  1992. fn interp_sin_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  1993. Ok((
  1994. // input arguments
  1995. vec![
  1996. (Keyword::Amplitude, Var::Float(1.0)),
  1997. (Keyword::Frequency, Var::Float(1.0)),
  1998. (Keyword::T, Var::Float(1.0)),
  1999. ],
  2000. // stack offset
  2001. 1,
  2002. ))
  2003. }
  2004. fn interp_sin_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2005. let amplitude: f32 = vm.stack_peek(1)?;
  2006. let frequency: f32 = vm.stack_peek(2)?;
  2007. let t: f32 = vm.stack_peek(3)?;
  2008. let res = interp::sin(amplitude, frequency, t);
  2009. Ok(Some(Var::Float(res)))
  2010. }
  2011. fn interp_bezier_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2012. Ok((
  2013. // input arguments
  2014. vec![
  2015. (Keyword::Coords, Var::Bool(false)),
  2016. (Keyword::T, Var::Float(1.0)),
  2017. ],
  2018. // stack offset
  2019. 1,
  2020. ))
  2021. }
  2022. fn interp_bezier_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2023. let default_mask: i32 = vm.stack_peek(3)?;
  2024. if !is_arg_given(default_mask, 1) {
  2025. error!("interp/bezier requires coords parameter");
  2026. return Err(Error::Native);
  2027. }
  2028. let coords = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2029. let t: f32 = vm.stack_peek(2)?;
  2030. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  2031. (x, y)
  2032. } else {
  2033. error!("coords 0 should be a Vec::V2D");
  2034. return Err(Error::Native);
  2035. };
  2036. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  2037. (x, y)
  2038. } else {
  2039. error!("coords 1 should be a Vec::V2D");
  2040. return Err(Error::Native);
  2041. };
  2042. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  2043. (x, y)
  2044. } else {
  2045. error!("coords 2 should be a Vec::V2D");
  2046. return Err(Error::Native);
  2047. };
  2048. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  2049. (x, y)
  2050. } else {
  2051. error!("coords 3 should be a Vec::V2D");
  2052. return Err(Error::Native);
  2053. };
  2054. let (x, y) = interp::bezier(&[x0, y0, x1, y1, x2, y2, x3, y3], t);
  2055. Ok(Some(Var::V2D(x, y)))
  2056. }
  2057. fn interp_bezier_tangent_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2058. Ok((
  2059. // input arguments
  2060. vec![
  2061. (Keyword::Coords, Var::Bool(false)),
  2062. (Keyword::T, Var::Float(1.0)),
  2063. ],
  2064. // stack offset
  2065. 1,
  2066. ))
  2067. }
  2068. fn interp_bezier_tangent_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2069. let default_mask: i32 = vm.stack_peek(3)?;
  2070. if !is_arg_given(default_mask, 1) {
  2071. error!("interp/bezier requires coords parameter");
  2072. return Err(Error::Native);
  2073. }
  2074. let coords = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2075. let t: f32 = vm.stack_peek(2)?;
  2076. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  2077. (x, y)
  2078. } else {
  2079. error!("coords 0 should be a Vec::V2D");
  2080. return Err(Error::Native);
  2081. };
  2082. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  2083. (x, y)
  2084. } else {
  2085. error!("coords 1 should be a Vec::V2D");
  2086. return Err(Error::Native);
  2087. };
  2088. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  2089. (x, y)
  2090. } else {
  2091. error!("coords 2 should be a Vec::V2D");
  2092. return Err(Error::Native);
  2093. };
  2094. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  2095. (x, y)
  2096. } else {
  2097. error!("coords 3 should be a Vec::V2D");
  2098. return Err(Error::Native);
  2099. };
  2100. let (x, y) = interp::bezier_tangent(&[x0, y0, x1, y1, x2, y2, x3, y3], t);
  2101. Ok(Some(Var::V2D(x, y)))
  2102. }
  2103. fn interp_ray_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2104. Ok((
  2105. // input arguments
  2106. vec![
  2107. (Keyword::Point, Var::V2D(0.0, 0.0)),
  2108. (Keyword::Direction, Var::V2D(1000.0, 1000.0)),
  2109. (Keyword::T, Var::Float(1.0)),
  2110. ],
  2111. // stack offset
  2112. 1,
  2113. ))
  2114. }
  2115. fn interp_ray_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2116. let point: (f32, f32) = vm.stack_peek(1)?;
  2117. let direction: (f32, f32) = vm.stack_peek(2)?;
  2118. let t: f32 = vm.stack_peek(3)?;
  2119. let (x, y) = interp::ray(point, direction, t);
  2120. Ok(Some(Var::V2D(x, y)))
  2121. }
  2122. fn interp_line_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2123. Ok((
  2124. // input arguments
  2125. vec![
  2126. (Keyword::From, Var::V2D(0.0, 0.0)),
  2127. (Keyword::To, Var::V2D(0.0, 0.0)),
  2128. (Keyword::Clamping, Var::Keyword(Keyword::False)),
  2129. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2130. (Keyword::T, Var::Float(1.0)),
  2131. ],
  2132. // stack offset
  2133. 1,
  2134. ))
  2135. }
  2136. fn interp_line_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2137. let from: (f32, f32) = vm.stack_peek(1)?;
  2138. let to: (f32, f32) = vm.stack_peek(2)?;
  2139. let clamping: Keyword = vm.stack_peek(3)?;
  2140. let mapping: Keyword = vm.stack_peek(4)?;
  2141. let t: f32 = vm.stack_peek(5)?;
  2142. let clamping = clamping == Keyword::True;
  2143. if let Some(mapping) = easing_from_keyword(mapping) {
  2144. let x = interp::scalar(from.0, to.0, mapping, clamping, t);
  2145. let y = interp::scalar(from.1, to.1, mapping, clamping, t);
  2146. Ok(Some(Var::V2D(x, y)))
  2147. } else {
  2148. error!("interp_line_execute");
  2149. Err(Error::Native)
  2150. }
  2151. }
  2152. fn interp_circle_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2153. Ok((
  2154. // input arguments
  2155. vec![
  2156. (Keyword::Position, Var::V2D(0.0, 0.0)),
  2157. (Keyword::Radius, Var::Float(1.0)),
  2158. (Keyword::T, Var::Float(0.0)),
  2159. ],
  2160. // stack offset
  2161. 1,
  2162. ))
  2163. }
  2164. fn interp_circle_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2165. let position: (f32, f32) = vm.stack_peek(1)?;
  2166. let radius: f32 = vm.stack_peek(2)?;
  2167. let t: f32 = vm.stack_peek(3)?;
  2168. let (x, y) = interp::circle(position, radius, t);
  2169. Ok(Some(Var::V2D(x, y)))
  2170. }
  2171. fn path_linear_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2172. Ok((
  2173. // input arguments
  2174. vec![
  2175. (Keyword::From, Var::V2D(0.0, 0.0)),
  2176. (Keyword::To, Var::V2D(10.0, 10.0)),
  2177. (Keyword::Steps, Var::Float(10.0)),
  2178. (Keyword::TStart, Var::Float(0.0)),
  2179. (Keyword::TEnd, Var::Float(1.0)),
  2180. (Keyword::Fn, Var::Bool(false)),
  2181. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2182. ],
  2183. // stack offset
  2184. 0,
  2185. ))
  2186. }
  2187. fn path_linear_execute(
  2188. vm: &mut Vm,
  2189. context: &mut Context,
  2190. program: &Program,
  2191. ) -> Result<Option<Var>> {
  2192. let default_mask: i32 = vm.stack_peek(8)?;
  2193. if !is_arg_given(default_mask, 6) {
  2194. error!("path_linear_execute requires fn argument");
  2195. return Err(Error::Native);
  2196. }
  2197. let from: (f32, f32) = vm.stack_peek(1)?;
  2198. let to: (f32, f32) = vm.stack_peek(2)?;
  2199. let steps: f32 = vm.stack_peek(3)?;
  2200. let t_start: f32 = vm.stack_peek(4)?;
  2201. let t_end: f32 = vm.stack_peek(5)?;
  2202. let fun: i32 = vm.stack_peek(6)?;
  2203. let mapping: Keyword = vm.stack_peek(7)?;
  2204. if let Some(mapping) = easing_from_keyword(mapping) {
  2205. path::linear(
  2206. vm,
  2207. context,
  2208. program,
  2209. fun as usize,
  2210. steps as i32,
  2211. t_start,
  2212. t_end,
  2213. from.0,
  2214. from.1,
  2215. to.0,
  2216. to.1,
  2217. mapping,
  2218. )?;
  2219. }
  2220. Ok(None)
  2221. }
  2222. fn path_circle_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2223. Ok((
  2224. // input arguments
  2225. vec![
  2226. (Keyword::Position, Var::V2D(0.0, 0.0)),
  2227. (Keyword::Radius, Var::Float(100.0)),
  2228. (Keyword::Steps, Var::Float(10.0)),
  2229. (Keyword::TStart, Var::Float(0.0)),
  2230. (Keyword::TEnd, Var::Float(1.0)),
  2231. (Keyword::Fn, Var::Bool(false)),
  2232. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2233. ],
  2234. // stack offset
  2235. 0,
  2236. ))
  2237. }
  2238. fn path_circle_execute(
  2239. vm: &mut Vm,
  2240. context: &mut Context,
  2241. program: &Program,
  2242. ) -> Result<Option<Var>> {
  2243. let default_mask: i32 = vm.stack_peek(8)?;
  2244. if !is_arg_given(default_mask, 6) {
  2245. error!("path_circle_execute requires fn argument");
  2246. return Err(Error::Native);
  2247. }
  2248. let position: (f32, f32) = vm.stack_peek(1)?;
  2249. let radius: f32 = vm.stack_peek(2)?;
  2250. let steps: f32 = vm.stack_peek(3)?;
  2251. let t_start: f32 = vm.stack_peek(4)?;
  2252. let t_end: f32 = vm.stack_peek(5)?;
  2253. let fun: i32 = vm.stack_peek(6)?;
  2254. let mapping: Keyword = vm.stack_peek(7)?;
  2255. if let Some(mapping) = easing_from_keyword(mapping) {
  2256. path::circular(
  2257. vm,
  2258. context,
  2259. program,
  2260. fun as usize,
  2261. steps as i32,
  2262. t_start,
  2263. t_end,
  2264. position.0,
  2265. position.1,
  2266. radius,
  2267. mapping,
  2268. )?;
  2269. }
  2270. Ok(None)
  2271. }
  2272. fn path_spline_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2273. Ok((
  2274. // input arguments
  2275. vec![
  2276. (Keyword::Coords, Var::Bool(false)),
  2277. (Keyword::Steps, Var::Float(10.0)),
  2278. (Keyword::TStart, Var::Float(0.0)),
  2279. (Keyword::TEnd, Var::Float(1.0)),
  2280. (Keyword::Fn, Var::Bool(false)),
  2281. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2282. ],
  2283. // stack offset
  2284. 0,
  2285. ))
  2286. }
  2287. fn path_spline_execute(
  2288. vm: &mut Vm,
  2289. context: &mut Context,
  2290. program: &Program,
  2291. ) -> Result<Option<Var>> {
  2292. let default_mask: i32 = vm.stack_peek(7)?;
  2293. if !is_arg_given(default_mask, 1) {
  2294. error!("path_spline_execute requires coords argument");
  2295. return Err(Error::Native);
  2296. }
  2297. if !is_arg_given(default_mask, 5) {
  2298. error!("path_spline_execute requires fn argument");
  2299. return Err(Error::Native);
  2300. }
  2301. let coords = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2302. let steps: f32 = vm.stack_peek(2)?;
  2303. let t_start: f32 = vm.stack_peek(3)?;
  2304. let t_end: f32 = vm.stack_peek(4)?;
  2305. let fun: i32 = vm.stack_peek(5)?;
  2306. let mapping: Keyword = vm.stack_peek(6)?;
  2307. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  2308. (x, y)
  2309. } else {
  2310. error!("coords 0 should be a Vec::V2D");
  2311. return Err(Error::Native);
  2312. };
  2313. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  2314. (x, y)
  2315. } else {
  2316. error!("coords 1 should be a Vec::V2D");
  2317. return Err(Error::Native);
  2318. };
  2319. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  2320. (x, y)
  2321. } else {
  2322. error!("coords 2 should be a Vec::V2D");
  2323. return Err(Error::Native);
  2324. };
  2325. if let Some(mapping) = easing_from_keyword(mapping) {
  2326. path::spline(
  2327. vm,
  2328. context,
  2329. program,
  2330. fun as usize,
  2331. steps as i32,
  2332. t_start,
  2333. t_end,
  2334. [x0, y0, x1, y1, x2, y2],
  2335. mapping,
  2336. )?;
  2337. }
  2338. Ok(None)
  2339. }
  2340. fn path_bezier_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2341. Ok((
  2342. // input arguments
  2343. vec![
  2344. (Keyword::Coords, Var::Bool(false)),
  2345. (Keyword::Steps, Var::Float(10.0)),
  2346. (Keyword::TStart, Var::Float(0.0)),
  2347. (Keyword::TEnd, Var::Float(1.0)),
  2348. (Keyword::Fn, Var::Bool(false)),
  2349. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2350. ],
  2351. // stack offset
  2352. 0,
  2353. ))
  2354. }
  2355. fn path_bezier_execute(
  2356. vm: &mut Vm,
  2357. context: &mut Context,
  2358. program: &Program,
  2359. ) -> Result<Option<Var>> {
  2360. let default_mask: i32 = vm.stack_peek(7)?;
  2361. if !is_arg_given(default_mask, 1) {
  2362. error!("path_bezier_execute requires coords argument");
  2363. return Err(Error::Native);
  2364. }
  2365. if !is_arg_given(default_mask, 5) {
  2366. error!("path_bezier_execute requires fn argument");
  2367. return Err(Error::Native);
  2368. }
  2369. let coords = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2370. let steps: f32 = vm.stack_peek(2)?;
  2371. let t_start: f32 = vm.stack_peek(3)?;
  2372. let t_end: f32 = vm.stack_peek(4)?;
  2373. let fun: i32 = vm.stack_peek(5)?;
  2374. let mapping: Keyword = vm.stack_peek(6)?;
  2375. let (x0, y0) = if let Var::V2D(x, y) = coords[0] {
  2376. (x, y)
  2377. } else {
  2378. error!("coords 0 should be a Vec::V2D");
  2379. return Err(Error::Native);
  2380. };
  2381. let (x1, y1) = if let Var::V2D(x, y) = coords[1] {
  2382. (x, y)
  2383. } else {
  2384. error!("coords 1 should be a Vec::V2D");
  2385. return Err(Error::Native);
  2386. };
  2387. let (x2, y2) = if let Var::V2D(x, y) = coords[2] {
  2388. (x, y)
  2389. } else {
  2390. error!("coords 2 should be a Vec::V2D");
  2391. return Err(Error::Native);
  2392. };
  2393. let (x3, y3) = if let Var::V2D(x, y) = coords[3] {
  2394. (x, y)
  2395. } else {
  2396. error!("coords 3 should be a Vec::V2D");
  2397. return Err(Error::Native);
  2398. };
  2399. if let Some(mapping) = easing_from_keyword(mapping) {
  2400. path::bezier(
  2401. vm,
  2402. context,
  2403. program,
  2404. fun as usize,
  2405. steps as i32,
  2406. t_start,
  2407. t_end,
  2408. [x0, y0, x1, y1, x2, y2, x3, y3],
  2409. mapping,
  2410. )?;
  2411. }
  2412. Ok(None)
  2413. }
  2414. fn repeat_symmetry_vertical_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2415. Ok((
  2416. // input arguments
  2417. vec![(Keyword::Fn, Var::Bool(false))],
  2418. // stack offset
  2419. 0,
  2420. ))
  2421. }
  2422. fn repeat_symmetry_vertical_execute(
  2423. vm: &mut Vm,
  2424. context: &mut Context,
  2425. program: &Program,
  2426. ) -> Result<Option<Var>> {
  2427. let default_mask: i32 = vm.stack_peek(2)?;
  2428. if !is_arg_given(default_mask, 1) {
  2429. error!("repeat_symmetry_vertical requires fn argument");
  2430. return Err(Error::Native);
  2431. }
  2432. let fun: i32 = vm.stack_peek(1)?;
  2433. repeat::symmetry_vertical(vm, context, program, fun as usize)?;
  2434. Ok(None)
  2435. }
  2436. fn repeat_symmetry_horizontal_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2437. Ok((
  2438. // input arguments
  2439. vec![(Keyword::Fn, Var::Bool(false))],
  2440. // stack offset
  2441. 0,
  2442. ))
  2443. }
  2444. fn repeat_symmetry_horizontal_execute(
  2445. vm: &mut Vm,
  2446. context: &mut Context,
  2447. program: &Program,
  2448. ) -> Result<Option<Var>> {
  2449. let default_mask: i32 = vm.stack_peek(2)?;
  2450. if !is_arg_given(default_mask, 1) {
  2451. error!("repeat_symmetry_horizontal requires fn argument");
  2452. return Err(Error::Native);
  2453. }
  2454. let fun: i32 = vm.stack_peek(1)?;
  2455. repeat::symmetry_horizontal(vm, context, program, fun as usize)?;
  2456. Ok(None)
  2457. }
  2458. fn repeat_symmetry_4_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2459. Ok((
  2460. // input arguments
  2461. vec![(Keyword::Fn, Var::Bool(false))],
  2462. // stack offset
  2463. 0,
  2464. ))
  2465. }
  2466. fn repeat_symmetry_4_execute(
  2467. vm: &mut Vm,
  2468. context: &mut Context,
  2469. program: &Program,
  2470. ) -> Result<Option<Var>> {
  2471. let default_mask: i32 = vm.stack_peek(2)?;
  2472. if !is_arg_given(default_mask, 1) {
  2473. error!("repeat_symmetry_4 requires fn argument");
  2474. return Err(Error::Native);
  2475. }
  2476. let fun: i32 = vm.stack_peek(1)?;
  2477. repeat::symmetry_4(vm, context, program, fun as usize)?;
  2478. Ok(None)
  2479. }
  2480. fn repeat_symmetry_8_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2481. Ok((
  2482. // input arguments
  2483. vec![(Keyword::Fn, Var::Bool(false))],
  2484. // stack offset
  2485. 0,
  2486. ))
  2487. }
  2488. fn repeat_symmetry_8_execute(
  2489. vm: &mut Vm,
  2490. context: &mut Context,
  2491. program: &Program,
  2492. ) -> Result<Option<Var>> {
  2493. let default_mask: i32 = vm.stack_peek(2)?;
  2494. if !is_arg_given(default_mask, 1) {
  2495. error!("repeat_symmetry_8 requires fn argument");
  2496. return Err(Error::Native);
  2497. }
  2498. let fun: i32 = vm.stack_peek(1)?;
  2499. repeat::symmetry_8(vm, context, program, fun as usize)?;
  2500. Ok(None)
  2501. }
  2502. fn repeat_rotate_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2503. Ok((
  2504. // input arguments
  2505. vec![
  2506. (Keyword::Fn, Var::Bool(false)),
  2507. (Keyword::Copies, Var::Float(3.0)),
  2508. ],
  2509. // stack offset
  2510. 0,
  2511. ))
  2512. }
  2513. fn repeat_rotate_execute(
  2514. vm: &mut Vm,
  2515. context: &mut Context,
  2516. program: &Program,
  2517. ) -> Result<Option<Var>> {
  2518. let default_mask: i32 = vm.stack_peek(3)?;
  2519. if !is_arg_given(default_mask, 1) {
  2520. error!("repeat_rotate requires fn argument");
  2521. return Err(Error::Native);
  2522. }
  2523. let fun: i32 = vm.stack_peek(1)?;
  2524. let copies: usize = vm.stack_peek(2)?;
  2525. repeat::rotate(vm, context, program, fun as usize, copies)?;
  2526. Ok(None)
  2527. }
  2528. fn repeat_rotate_mirrored_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2529. Ok((
  2530. // input arguments
  2531. vec![
  2532. (Keyword::Fn, Var::Bool(false)),
  2533. (Keyword::Copies, Var::Float(3.0)),
  2534. ],
  2535. // stack offset
  2536. 0,
  2537. ))
  2538. }
  2539. fn repeat_rotate_mirrored_execute(
  2540. vm: &mut Vm,
  2541. context: &mut Context,
  2542. program: &Program,
  2543. ) -> Result<Option<Var>> {
  2544. let default_mask: i32 = vm.stack_peek(3)?;
  2545. if !is_arg_given(default_mask, 1) {
  2546. error!("repeat_rotate_mirrored requires fn argument");
  2547. return Err(Error::Native);
  2548. }
  2549. let fun: i32 = vm.stack_peek(1)?;
  2550. let copies: usize = vm.stack_peek(2)?;
  2551. repeat::rotate_mirrored(vm, context, program, fun as usize, copies)?;
  2552. Ok(None)
  2553. }
  2554. fn focal_build_generic_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2555. Ok((
  2556. // input arguments
  2557. vec![
  2558. (Keyword::Mapping, Var::Keyword(Keyword::Linear)),
  2559. (Keyword::Position, Var::V2D(0.0, 0.0)),
  2560. (Keyword::Distance, Var::Float(1.0)),
  2561. (Keyword::TransformPosition, Var::Keyword(Keyword::False)),
  2562. ],
  2563. // stack offset
  2564. 1,
  2565. ))
  2566. }
  2567. fn focal_build_generic_execute(vm: &mut Vm, focal_type: focal::FocalType) -> Result<Option<Var>> {
  2568. let mapping: Keyword = vm.stack_peek(1)?;
  2569. let position: (f32, f32) = vm.stack_peek(2)?;
  2570. let distance: f32 = vm.stack_peek(3)?;
  2571. let transform_pos: Keyword = vm.stack_peek(4)?;
  2572. let transform_pos = transform_pos == Keyword::True;
  2573. if let Some(mapping) = easing_from_keyword(mapping) {
  2574. Ok(Some(Var::FocalState(focal::FocalStateStruct {
  2575. focal_type,
  2576. mapping,
  2577. position,
  2578. distance,
  2579. transform_pos,
  2580. })))
  2581. } else {
  2582. error!("focal_build_generic _execute");
  2583. Err(Error::Native)
  2584. }
  2585. }
  2586. fn focal_build_point_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2587. focal_build_generic_execute(vm, focal::FocalType::Point)
  2588. }
  2589. fn focal_build_vline_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2590. focal_build_generic_execute(vm, focal::FocalType::VLine)
  2591. }
  2592. fn focal_build_hline_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2593. focal_build_generic_execute(vm, focal::FocalType::HLine)
  2594. }
  2595. fn focal_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2596. Ok((
  2597. // input arguments
  2598. vec![
  2599. (Keyword::From, Var::Bool(false)),
  2600. (Keyword::Position, Var::V2D(0.0, 0.0)),
  2601. ],
  2602. // stack offset
  2603. 1,
  2604. ))
  2605. }
  2606. fn focal_value_execute(vm: &mut Vm, context: &mut Context) -> Result<Option<Var>> {
  2607. let default_mask: i32 = vm.stack_peek(3)?;
  2608. if !is_arg_given(default_mask, 1) {
  2609. error!("focal/value requires a from parameter");
  2610. return Err(Error::Native);
  2611. }
  2612. let focal_state_struct = stack_peek_focal_state_struct(&vm.stack, vm.sp, 1)?;
  2613. let position: (f32, f32) = vm.stack_peek(2)?;
  2614. let res = focal_state_struct.value(context, position);
  2615. // error!("focal/value: {} position: {:?} focal_state_struct: {:?}", res, &position, &focal_state_struct);
  2616. Ok(Some(Var::Float(res)))
  2617. }
  2618. fn bitmap_each_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2619. Ok((
  2620. // input arguments
  2621. vec![
  2622. (Keyword::From, Var::Bool(false)),
  2623. (Keyword::Position, Var::V2D(500.0, 500.0)),
  2624. (Keyword::Width, Var::Float(1000.0)),
  2625. (Keyword::Height, Var::Float(1000.0)),
  2626. (Keyword::Fn, Var::Bool(false)),
  2627. (Keyword::ShuffleSeed, Var::Float(0.0)),
  2628. ],
  2629. // stack offset
  2630. 0,
  2631. ))
  2632. }
  2633. fn bitmap_each_execute(
  2634. vm: &mut Vm,
  2635. context: &mut Context,
  2636. program: &Program,
  2637. ) -> Result<Option<Var>> {
  2638. let default_mask: i32 = vm.stack_peek(7)?;
  2639. if !is_arg_given(default_mask, 1) {
  2640. error!("bitmap/each requires a from parameter");
  2641. return Err(Error::Native);
  2642. }
  2643. if !is_arg_given(default_mask, 5) {
  2644. error!("bitmap/each requires a fn parameter");
  2645. return Err(Error::Native);
  2646. }
  2647. let from: Iname = vm.stack_peek(1)?;
  2648. let position: (f32, f32) = vm.stack_peek(2)?;
  2649. let width: f32 = vm.stack_peek(3)?;
  2650. let height: f32 = vm.stack_peek(4)?;
  2651. let fun: i32 = vm.stack_peek(5)?;
  2652. let shuffle_seed: Option<f32> = if is_arg_given(default_mask, 6) {
  2653. let seed: f32 = vm.stack_peek(6)?;
  2654. Some(seed)
  2655. } else {
  2656. None
  2657. };
  2658. bitmap::each(
  2659. vm,
  2660. context,
  2661. program,
  2662. fun as usize,
  2663. from,
  2664. position,
  2665. width,
  2666. height,
  2667. shuffle_seed,
  2668. )?;
  2669. Ok(None)
  2670. }
  2671. fn bitmap_value_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2672. Ok((
  2673. // input arguments
  2674. vec![
  2675. (Keyword::From, Var::Bool(false)),
  2676. (Keyword::Position, Var::V2D(0.0, 0.0)),
  2677. ],
  2678. // stack offset
  2679. 1,
  2680. ))
  2681. }
  2682. fn bitmap_value_execute(
  2683. vm: &mut Vm,
  2684. context: &mut Context,
  2685. program: &Program,
  2686. ) -> Result<Option<Var>> {
  2687. let default_mask: i32 = vm.stack_peek(3)?;
  2688. if !is_arg_given(default_mask, 1) {
  2689. error!("bitmap/value requires a from parameter");
  2690. return Err(Error::Native);
  2691. }
  2692. let from: Iname = vm.stack_peek(1)?;
  2693. let position: (f32, f32) = vm.stack_peek(2)?;
  2694. let col = bitmap::value(context, program, from, position)?;
  2695. Ok(Some(Var::Colour(col)))
  2696. }
  2697. fn bitmap_width_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2698. Ok((
  2699. // input arguments
  2700. vec![
  2701. (Keyword::From, Var::Bool(false)),
  2702. ],
  2703. // stack offset
  2704. 1,
  2705. ))
  2706. }
  2707. fn bitmap_width_execute(
  2708. vm: &mut Vm,
  2709. context: &mut Context,
  2710. program: &Program,
  2711. ) -> Result<Option<Var>> {
  2712. let default_mask: i32 = vm.stack_peek(2)?;
  2713. if !is_arg_given(default_mask, 1) {
  2714. error!("bitmap/width requires a from parameter");
  2715. return Err(Error::Native);
  2716. }
  2717. let from: Iname = vm.stack_peek(1)?;
  2718. let width = bitmap::width(context, program, from)?;
  2719. Ok(Some(Var::Float(width)))
  2720. }
  2721. fn bitmap_height_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2722. Ok((
  2723. // input arguments
  2724. vec![
  2725. (Keyword::From, Var::Bool(false)),
  2726. ],
  2727. // stack offset
  2728. 1,
  2729. ))
  2730. }
  2731. fn bitmap_height_execute(
  2732. vm: &mut Vm,
  2733. context: &mut Context,
  2734. program: &Program,
  2735. ) -> Result<Option<Var>> {
  2736. let default_mask: i32 = vm.stack_peek(2)?;
  2737. if !is_arg_given(default_mask, 1) {
  2738. error!("bitmap/height requires a from parameter");
  2739. return Err(Error::Native);
  2740. }
  2741. let from: Iname = vm.stack_peek(1)?;
  2742. let height = bitmap::height(context, program, from)?;
  2743. Ok(Some(Var::Float(height)))
  2744. }
  2745. fn gen_stray_int_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2746. Ok((
  2747. // input arguments
  2748. vec![
  2749. (Keyword::From, Var::Float(1.0)),
  2750. (Keyword::By, Var::Float(0.2)),
  2751. ],
  2752. // stack offset
  2753. 1,
  2754. ))
  2755. }
  2756. fn gen_stray_int_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2757. let from: f32 = vm.stack_peek(1)?;
  2758. let by: f32 = vm.stack_peek(2)?;
  2759. let by = mathutil::absf(by);
  2760. let value = vm.prng_state.next_f32_range(from - by, from + by);
  2761. let value = value.floor();
  2762. Ok(Some(Var::Float(value)))
  2763. }
  2764. fn gen_stray_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2765. Ok((
  2766. // input arguments
  2767. vec![
  2768. (Keyword::From, Var::Float(1.0)),
  2769. (Keyword::By, Var::Float(0.2)),
  2770. ],
  2771. // stack offset
  2772. 1,
  2773. ))
  2774. }
  2775. fn gen_stray_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2776. let from: f32 = vm.stack_peek(1)?;
  2777. let by: f32 = vm.stack_peek(2)?;
  2778. let by = mathutil::absf(by);
  2779. let value = vm.prng_state.next_f32_range(from - by, from + by);
  2780. Ok(Some(Var::Float(value)))
  2781. }
  2782. fn gen_stray_2d_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2783. Ok((
  2784. // input arguments
  2785. vec![
  2786. (Keyword::From, Var::V2D(10.0, 10.0)),
  2787. (Keyword::By, Var::V2D(1.0, 1.0)),
  2788. ],
  2789. // stack offset
  2790. 1,
  2791. ))
  2792. }
  2793. fn gen_stray_2d_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2794. if !vm.building_with_trait_within_vector {
  2795. error!("gen_stray_2d should always be called with vm.building_with_trait_within_vector");
  2796. return Err(Error::Native);
  2797. }
  2798. let from: (f32, f32) = vm.stack_peek(1)?;
  2799. let by: (f32, f32) = vm.stack_peek(2)?;
  2800. let index = vm.trait_within_vector_index;
  2801. let by_index;
  2802. let from_index;
  2803. if index == 0 {
  2804. by_index = mathutil::absf(by.0);
  2805. from_index = from.0;
  2806. } else if index == 1 {
  2807. by_index = mathutil::absf(by.1);
  2808. from_index = from.1;
  2809. } else {
  2810. error!("gen_stray_2d invalid trait_within_vector_index value");
  2811. return Err(Error::Native);
  2812. }
  2813. // pick a scalar between min and max
  2814. let value = vm
  2815. .prng_state
  2816. .next_f32_range(from_index - by_index, from_index + by_index);
  2817. Ok(Some(Var::Float(value)))
  2818. }
  2819. fn gen_stray_3d_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2820. Ok((
  2821. // input arguments
  2822. vec![
  2823. (Keyword::From, Var::Bool(false)),
  2824. (Keyword::By, Var::Bool(false)),
  2825. ],
  2826. // stack offset
  2827. 1,
  2828. ))
  2829. }
  2830. fn gen_stray_3d_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2831. if !vm.building_with_trait_within_vector {
  2832. error!("gen_stray_3d should always be called with vm.building_with_trait_within_vector");
  2833. return Err(Error::Native);
  2834. }
  2835. let default_mask: i32 = vm.stack_peek(3)?;
  2836. if !is_arg_given(default_mask, 1) {
  2837. error!("gen/stray-3d requires a from parameter");
  2838. return Err(Error::Native);
  2839. }
  2840. if !is_arg_given(default_mask, 2) {
  2841. error!("gen/stray-3d requires a by parameter");
  2842. return Err(Error::Native);
  2843. }
  2844. let from = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2845. let by = stack_peek_vars(&vm.stack, vm.sp, 2)?;
  2846. let index = vm.trait_within_vector_index;
  2847. let from = if let Some(var) = from.get(index) {
  2848. Var::get_float_value(&var)?
  2849. } else {
  2850. error!("gen_stray_3d requires both from and by parameters");
  2851. return Err(Error::Native);
  2852. };
  2853. let by = if let Some(var) = by.get(index) {
  2854. Var::get_float_value(&var)?
  2855. } else {
  2856. error!("gen_stray_3d requires both from and by parameters");
  2857. return Err(Error::Native);
  2858. };
  2859. // pick a scalar between min and max
  2860. let value = vm.prng_state.next_f32_range(from - by, from + by);
  2861. Ok(Some(Var::Float(value)))
  2862. }
  2863. fn gen_stray_4d_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2864. Ok((
  2865. // input arguments
  2866. vec![
  2867. (Keyword::From, Var::Bool(false)),
  2868. (Keyword::By, Var::Bool(false)),
  2869. ],
  2870. // stack offset
  2871. 1,
  2872. ))
  2873. }
  2874. fn gen_stray_4d_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2875. if !vm.building_with_trait_within_vector {
  2876. error!("gen_stray_4d should always be called with vm.building_with_trait_within_vector");
  2877. return Err(Error::Native);
  2878. }
  2879. let default_mask: i32 = vm.stack_peek(3)?;
  2880. if !is_arg_given(default_mask, 1) {
  2881. error!("gen/stray-4d requires a from parameter");
  2882. return Err(Error::Native);
  2883. }
  2884. if !is_arg_given(default_mask, 2) {
  2885. error!("gen/stray-4d requires a by parameter");
  2886. return Err(Error::Native);
  2887. }
  2888. let from = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2889. let by = stack_peek_vars(&vm.stack, vm.sp, 2)?;
  2890. let index = vm.trait_within_vector_index;
  2891. let from = if let Some(var) = from.get(index) {
  2892. Var::get_float_value(&var)?
  2893. } else {
  2894. error!("gen_stray_4d requires both from and by parameters");
  2895. return Err(Error::Native);
  2896. };
  2897. let by = if let Some(var) = by.get(index) {
  2898. Var::get_float_value(&var)?
  2899. } else {
  2900. error!("gen_stray_4d requires both from and by parameters");
  2901. return Err(Error::Native);
  2902. };
  2903. // pick a scalar between min and max
  2904. let value = vm.prng_state.next_f32_range(from - by, from + by);
  2905. Ok(Some(Var::Float(value)))
  2906. }
  2907. fn gen_int_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2908. Ok((
  2909. // input arguments
  2910. vec![
  2911. (Keyword::Min, Var::Float(0.0)),
  2912. (Keyword::Max, Var::Float(1000.0)),
  2913. ],
  2914. // stack offset
  2915. 1,
  2916. ))
  2917. }
  2918. fn gen_int_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2919. let min: f32 = vm.stack_peek(1)?;
  2920. let max: f32 = vm.stack_peek(2)?;
  2921. // pick a scalar between min and max
  2922. let value = vm.prng_state.next_f32_range(min, max + 1.0);
  2923. Ok(Some(Var::Float(value.floor())))
  2924. }
  2925. fn gen_scalar_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2926. Ok((
  2927. // input arguments
  2928. vec![
  2929. (Keyword::Min, Var::Float(0.0)),
  2930. (Keyword::Max, Var::Float(1.0)),
  2931. ],
  2932. // stack offset
  2933. 1,
  2934. ))
  2935. }
  2936. fn gen_scalar_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2937. let min: f32 = vm.stack_peek(1)?;
  2938. let max: f32 = vm.stack_peek(2)?;
  2939. // pick a scalar between min and max
  2940. let value = vm.prng_state.next_f32_range(min, max);
  2941. Ok(Some(Var::Float(value)))
  2942. }
  2943. fn gen_2d_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2944. Ok((
  2945. // input arguments
  2946. vec![
  2947. (Keyword::Min, Var::Float(0.0)),
  2948. (Keyword::Max, Var::Float(1.0)),
  2949. ],
  2950. // stack offset
  2951. 1,
  2952. ))
  2953. }
  2954. fn gen_2d_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2955. let min: f32 = vm.stack_peek(1)?;
  2956. let max: f32 = vm.stack_peek(2)?;
  2957. let x = vm.prng_state.next_f32_range(min, max);
  2958. let y = vm.prng_state.next_f32_range(min, max);
  2959. Ok(Some(Var::V2D(x, y)))
  2960. }
  2961. fn gen_select_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2962. Ok((
  2963. // input arguments
  2964. vec![(Keyword::From, Var::Bool(false))],
  2965. // stack offset
  2966. 1,
  2967. ))
  2968. }
  2969. fn gen_select_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2970. let default_mask: i32 = vm.stack_peek(2)?;
  2971. if !is_arg_given(default_mask, 1) {
  2972. error!("gen/select requires a from parameter");
  2973. return Err(Error::Native);
  2974. }
  2975. let from = stack_peek_vars(&vm.stack, vm.sp, 1)?;
  2976. let index = vm.prng_state.next_usize_range(0, from.len());
  2977. Ok(Some(from[index].clone()))
  2978. }
  2979. fn gen_col_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
  2980. Ok((
  2981. // input arguments
  2982. vec![(Keyword::Alpha, Var::Float(1.0))],
  2983. // stack offset
  2984. 1,
  2985. ))
  2986. }
  2987. fn gen_col_execute(vm: &mut Vm) -> Result<Option<Var>> {
  2988. let default_mask: i32 = vm.stack_peek(2)?;
  2989. let alpha: f32 = if is_arg_given(default_mask, 1) {
  2990. vm.stack_peek(1)?
  2991. } else {
  2992. // no alpha was given so generate a random value
  2993. vm.prng_state.next_f32_range(0.0, 1.0)
  2994. };
  2995. Ok(Some(Var::Colour(Colour::new(
  2996. ColourFormat::Rgb,
  2997. vm.prng_state.next_f32_range(0.0, 1.0),
  2998. vm.prng_state.next_f32_range(0.0, 1.0),
  2999. vm.prng_state.next_f32_range(0.0, 1.0),
  3000. alpha,
  3001. ))))
  3002. }
  3003. #[cfg(test)]
  3004. mod tests {
  3005. use super::*;
  3006. use crate::colour::ColourFormat;
  3007. use crate::geometry::RENDER_PACKET_FLOAT_PER_VERTEX;
  3008. use crate::vm::tests::*;
  3009. use crate::vm::*;
  3010. fn is_col_rgb(s: &str, r: f32, g: f32, b: f32, alpha: f32) {
  3011. let mut vm: Vm = Default::default();
  3012. let mut context: Context = Default::default();
  3013. if let Var::Colour(col) = vm_exec(&mut vm, &mut context, s) {
  3014. assert_eq!(col.format, ColourFormat::Rgb);
  3015. assert_eq!(col.e0, r);
  3016. assert_eq!(col.e1, g);
  3017. assert_eq!(col.e2, b);
  3018. assert_eq!(col.e3, alpha);
  3019. }
  3020. }
  3021. // get render packet 0's geometry length
  3022. fn rp0_num_vertices(context: &Context, expected_num_vertices: usize) {
  3023. assert_eq!(
  3024. context.get_render_packet_geo_len(0),
  3025. expected_num_vertices * RENDER_PACKET_FLOAT_PER_VERTEX
  3026. );
  3027. }
  3028. // #[test]
  3029. fn dev_rendering_fns() {
  3030. let mut vm: Vm = Default::default();
  3031. let mut context: Context = Default::default();
  3032. vm_run(&mut vm, &mut context, "(line width: 33 from: [2 3] to: [400 500] colour: (col/rgb r: 0 g: 0.1 b: 0.2 alpha: 0.3))");
  3033. // vm_run(&mut vm, "(line width: 0 from: [2 3] to: [400 500] brush: brush/b colour: (col/rgb r: 0 g: 0.1 b: 0.2 alpha: 0.3))");
  3034. // vm_run(&mut vm, "(line brush: brush/b)");
  3035. // vm_run(&mut vm, "(line brush: brush/b) (rect width: 10 height: 30)");
  3036. let res = vm.top_stack_value().unwrap();
  3037. if let Var::Debug(s) = res {
  3038. assert_eq!(s, "x");
  3039. } else {
  3040. assert_eq!(false, true);
  3041. }
  3042. rp0_num_vertices(&context, 4);
  3043. }
  3044. #[test]
  3045. fn test_native_pack() {
  3046. let mut res: String = "".to_string();
  3047. Native::ColGetAlpha.pack(&mut res).unwrap();
  3048. assert_eq!("col/get-alpha", res);
  3049. }
  3050. #[test]
  3051. fn test_native_unpack() {
  3052. let (res, _rem) = Native::unpack("col/get-alpha").unwrap();
  3053. assert_eq!(res, Native::ColGetAlpha);
  3054. }
  3055. #[test]
  3056. fn test_probe() {
  3057. is_debug_str("(probe scalar: 0.4)", "0.4");
  3058. is_debug_str(
  3059. "(probe scalar: 0.4) (probe scalar: 0.7) (probe scalar: 0.9)",
  3060. "0.4 0.7 0.9",
  3061. );
  3062. }
  3063. #[test]
  3064. fn test_col_rgb() {
  3065. is_col_rgb(
  3066. "(col/rgb r: 0.1 g: 0.2 b: 0.3 alpha: 0.4)",
  3067. 0.1,
  3068. 0.2,
  3069. 0.3,
  3070. 0.4,
  3071. );
  3072. }
  3073. #[test]
  3074. fn test_nth() {
  3075. is_float("(define v [1 2 3 4]) (nth from: v n: 0)", 1.0);
  3076. is_float("(define v [1 2 3 4]) (nth from: v n: 1)", 2.0);
  3077. is_float("(define v [1 2 3 4]) (nth from: v n: 2)", 3.0);
  3078. is_float("(define v [1 2 3 4]) (nth from: v n: 3)", 4.0);
  3079. is_float("(define v [9 8]) (nth from: v n: 0)", 9.0);
  3080. is_float("(define v [9 8]) (nth from: v n: 1)", 8.0);
  3081. }
  3082. #[test]
  3083. pub fn bug_nth() {
  3084. is_debug_str(
  3085. "(define vs [5 6 7])
  3086. (probe scalar: (nth from: vs n: 0))",
  3087. "5",
  3088. );
  3089. }
  3090. #[test]
  3091. fn test_vector_length() {
  3092. is_int("(define v []) (++ v 100) (vector/length vector: v)", 1);
  3093. is_int("(define v [1]) (++ v 100) (vector/length vector: v)", 2);
  3094. is_int("(define v [1 2]) (++ v 100) (vector/length vector: v)", 3);
  3095. is_int("(define v [1 2 3]) (++ v 100) (vector/length vector: v)", 4);
  3096. is_int(
  3097. "(define v [1 2 3 4]) (++ v 100) (vector/length vector: v)",
  3098. 5,
  3099. );
  3100. is_int(
  3101. "(define v []) (++ v 4) (++ v 3) (++ v 2) (++ v 1) (++ v 0) (vector/length vector: v)",
  3102. 5,
  3103. );
  3104. is_int(
  3105. "(define v [1 2]) (++ v 98) (++ v 99) (++ v 100) (vector/length vector: v)",
  3106. 5,
  3107. );
  3108. }
  3109. #[test]
  3110. fn test_math() {
  3111. is_float("(math/clamp value: 3 min: 2 max: 5)", 3.0);
  3112. is_float("(math/clamp value: 1 min: 2 max: 5)", 2.0);
  3113. is_float("(math/clamp value: 8 min: 2 max: 5)", 5.0);
  3114. is_float("(math/radians->degrees angle: 0.3)", 17.188734);
  3115. is_float("(math/cos angle: 0.7)", 0.7648422);
  3116. is_float("(math/sin angle: 0.9)", 0.7833269);
  3117. }
  3118. #[test]
  3119. fn dev_new_args() {
  3120. is_float("(math/clamp value: 3 min: 2 max: 5)", 3.0);
  3121. is_float("(math/clamp value: 1 min: 2 max: 5)", 2.0);
  3122. is_float("(math/clamp value: 8 min: 2 max: 5)", 5.0);
  3123. }
  3124. }