Skip to content

Upgrade Python language bindings to v1.4.1 #2459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmake/pocketpy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ message("BUILD_WITH_PYTHON: ${BUILD_WITH_PYTHON}")
if(BUILD_WITH_PYTHON)

add_subdirectory(${THIRDPARTY_DIR}/pocketpy)
target_compile_definitions(pocketpy PRIVATE PK_ENABLE_OS=0)
include_directories(${THIRDPARTY_DIR}/pocketpy/include)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand Down
44 changes: 13 additions & 31 deletions src/api/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ struct CachedNames{
pkpy_CName MENU;
} N;

// duplicate a pkpy_CString to a null-terminated c string
static char* cstrdup(pkpy_CString cs){
char* s = (char*)malloc(cs.size + 1);
memcpy(s, cs.data, cs.size);
s[cs.size] = '\0';
return s;
}

static void pkpy_setglobal_2(pkpy_vm* vm, const char* name){
pkpy_setglobal(vm, pkpy_name(name));
}
Expand Down Expand Up @@ -105,9 +97,7 @@ static int py_trace(pkpy_vm* vm)
return 0;
}

char* message_s = cstrdup(message);
tic_api_trace(tic, message_s, (u8) color);
free(message_s);
tic_api_trace(tic, message, (u8) color);
return 0;
}

Expand Down Expand Up @@ -344,9 +334,7 @@ static int py_font(pkpy_vm* vm)
else
{
u8 chromakey = (u8) chromakey_raw;
char* text_s = cstrdup(text);
s32 size = tic_api_font(tic, text_s, x, y, &chromakey, 1, width, height, fixed, scale, alt);
free(text_s);
s32 size = tic_api_font(tic, text, x, y, &chromakey, 1, width, height, fixed, scale, alt);
pkpy_push_int(vm, size);
}

Expand All @@ -364,7 +352,7 @@ static int py_key(pkpy_vm* vm)
return 0;

if (key_id >= tic_keys_count) {
pkpy_error(vm, "tic80-panic!", pkpy_string("unknown keyboard code\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("unknown keyboard code\n"));
return 0;
}

Expand All @@ -388,7 +376,7 @@ static int py_keyp(pkpy_vm* vm)
return 0;

if (key_id >= tic_keys_count) {
pkpy_error(vm, "tic80-panic!", pkpy_string("unknown keyboard code\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("unknown keyboard code\n"));
return 0;
}

Expand Down Expand Up @@ -598,7 +586,7 @@ static int py_music(pkpy_vm* vm) {
return 0;

if (track > MUSIC_TRACKS - 1 )
pkpy_error(vm, "tic80-panic!", pkpy_string("invalid music track index\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("invalid music track index\n"));

//stop the music first I guess
tic_api_music(tic, -1, 0, 0, false, false, -1, -1);
Expand Down Expand Up @@ -715,7 +703,7 @@ static int py_pmem(pkpy_vm* vm) {
return 0;

if (index >= TIC_PERSISTENT_SIZE) {
pkpy_error(vm, "tic80-panic!", pkpy_string("invalid persistent tic index\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("invalid persistent tic index\n"));
return 0;
}

Expand Down Expand Up @@ -825,10 +813,7 @@ static int py_print(pkpy_vm* vm) {
return 0;
}

char* text_s = cstrdup(text);
s32 size = tic_api_print(tic, text_s, x, y, color, fixed, scale, alt);
free(text_s);

s32 size = tic_api_print(tic, text, x, y, color, fixed, scale, alt);
pkpy_push_int(vm, size);
return 1;
}
Expand Down Expand Up @@ -883,7 +868,7 @@ static int py_sfx(pkpy_vm* vm)
int sfx_id;

bool parse_note = false;
char* string_note = NULL;
const char* string_note = NULL;
int int_note;

int duration;
Expand All @@ -895,9 +880,7 @@ static int py_sfx(pkpy_vm* vm)

if (pkpy_is_string(vm, 1)) {
parse_note = true;
pkpy_CString tmp;
pkpy_to_string(vm, 1, &tmp);
if(tmp.size) string_note = cstrdup(tmp);
pkpy_to_string(vm, 1, &string_note);
} else {
pkpy_to_int(vm, 1, &int_note);
}
Expand All @@ -913,7 +896,7 @@ static int py_sfx(pkpy_vm* vm)

if (parse_note) {
if(!tic_tool_parse_note(string_note, &note, &octave)) {
pkpy_error(vm, "tic80-panic!", pkpy_string("invalid note, should like C#4\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("invalid note, should like C#4\n"));
goto cleanup; //error in future;
}

Expand All @@ -923,12 +906,12 @@ static int py_sfx(pkpy_vm* vm)
}

if (channel < 0 || channel >= TIC_SOUND_CHANNELS) {
pkpy_error(vm, "tic80-panic!", pkpy_string("unknown channel\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("unknown channel\n"));
goto cleanup;
}

if (sfx_id >= SFX_COUNT) {
pkpy_error(vm, "tic80-panic!", pkpy_string("unknown sfx index\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("unknown sfx index\n"));
goto cleanup;
}

Expand All @@ -937,7 +920,6 @@ static int py_sfx(pkpy_vm* vm)
tic_api_sfx(tic, sfx_id, note, octave, duration, channel, volume & 0xf, volume & 0xf, speed);

cleanup :
if (string_note != NULL) free(string_note);
return 0;
}

Expand Down Expand Up @@ -1000,7 +982,7 @@ static int py_sync(pkpy_vm* vm)
return 0;

if (bank < 0 || bank >= TIC_BANKS) {
pkpy_error(vm, "tic80-panic!", pkpy_string("sync() error, invalid bank\n"));
pkpy_error(vm, "RuntimeError", pkpy_string("sync() error, invalid bank\n"));
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion vendor/pocketpy