Coder Social home page Coder Social logo

Passing a struct about my_basic HOT 4 CLOSED

jacmoe avatar jacmoe commented on June 8, 2024
Passing a struct

from my_basic.

Comments (4)

paladin-t avatar paladin-t commented on June 8, 2024 2

There is a byte array field in the mb_value_t struct, which you can store some bytes in. For example passing a struct (sizeof(64) bytes) through MY-BASIC:

typedef struct Tag {
	char c0[64];
} Tag;

mb_static_assert(sizeof(mb_val_bytes_t) >= sizeof(Tag));

static int test0(struct mb_interpreter_t* s, void** l) {
	int result = MB_FUNC_OK;
	mb_value_t val;

	mb_assert(s && l);

	mb_check(mb_attempt_open_bracket(s, l));

	mb_check(mb_attempt_close_bracket(s, l));

	Tag tag;
	for (int i = 0; i < sizeof(tag.c0) - 1; ++i)
		tag.c0[i] = '*';
	tag.c0[sizeof(tag.c0) - 1] = '\0';
	val.type = MB_DT_USERTYPE;
	memset(&val.value.bytes, 0, sizeof(mb_val_bytes_t));
	memcpy(&val.value.bytes, &tag, sizeof(Tag));

	mb_check(mb_push_value(s, l, val));

	return result;
}

static int test1(struct mb_interpreter_t* s, void** l) {
	int result = MB_FUNC_OK;
	mb_value_t val;

	mb_assert(s && l);

	mb_check(mb_attempt_open_bracket(s, l));

	mb_make_nil(val);
	mb_check(mb_pop_value(s, l, &val));

	mb_check(mb_attempt_close_bracket(s, l));

	Tag tag;
	memcpy(&tag, &val.value.bytes, sizeof(Tag));

	printf(tag.c0);

	return result;
}

Test it with:

test1(test0())

By default, the byte array typed mb_val_bytes_t is defined as large as maximum of sizes of void*, unsigned long, int_t, real_t, in my_basic.h. Make sure you've redefined it large enough to store your struct:

typedef unsigned char mb_val_bytes_t[64];

from my_basic.

paladin-t avatar paladin-t commented on June 8, 2024 1

I've documented this feature in the wiki.

from my_basic.

jacmoe avatar jacmoe commented on June 8, 2024 1

I will try this! Thanks a lot ๐Ÿ˜ƒ

I have to say, that the MY-BASIC documentation is almost too good! ๐Ÿฅ‡

from my_basic.

jacmoe avatar jacmoe commented on June 8, 2024

I just finished binding my SpriteSheets to MY-BASIC - it works wonderfully.
For me, this issue has been resolved.
Thanks a lot! :)

from my_basic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.