MCPcopy Create free account
hub / github.com/DavidColson/Polybox / NewUserData

Function NewUserData

source/userdata.cpp:255–303  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

253// ***********************************************************************
254
255i32 NewUserData(lua_State* L) {
256 i32 nArgs = lua_gettop(L);
257 const char* typeStr = luaL_checkstring(L, 1);
258 i32 width = (i32)luaL_checkinteger(L, 2);
259 i32 height = 1;
260 String dataStr;
261
262 if (nArgs > 2) {
263 if (lua_isnumber(L, 3)) {
264 height = (i32)lua_tointeger(L, 3);
265 }
266 else if (lua_isstring(L, 3)) {
267 dataStr = lua_tostring(L, 3);
268 }
269 else {
270 luaL_error(L, "Unexpected 3rd argument to userdata, should be integer or string");
271 }
272 }
273
274 if (nArgs > 3) {
275 u64 len;
276 dataStr.pData = (char*)luaL_checklstring(L, 4, &len);
277 dataStr.length = (i64)len;
278 }
279
280 Type type;
281 if (strcmp(typeStr, "f32") == 0) {
282 type = Type::Float32;
283 }
284 else if (strcmp(typeStr, "i32") == 0) {
285 type = Type::Int32;
286 }
287 else if (strcmp(typeStr, "i16") == 0) {
288 type = Type::Int16;
289 }
290 else if (strcmp(typeStr, "u8") == 0) {
291 type = Type::Uint8;
292 }
293 else {
294 luaL_error(L, "invalid type given to userdata creation %s", typeStr);
295 return 0;
296 }
297 UserData* pUserData = AllocUserData(L, type, width, height);
298
299 if (dataStr.length > 0) {
300 ParseUserDataDataString(L, dataStr, pUserData);
301 }
302 return 1;
303}
304
305// ***********************************************************************
306

Callers

nothing calls this directly

Calls 2

AllocUserDataFunction · 0.85
ParseUserDataDataStringFunction · 0.85

Tested by

no test coverage detected