Fixed apps not working most of the time. Ultimately, storing a pointer to the database using JS_NewInt64 was lossy and a bad idea. Also, remove use of JNI since we're only starting tildefriends as its own process now.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4215 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-03-11 13:57:17 +00:00
parent 2a3b1a1e33
commit 10bfa65a4e
9 changed files with 19 additions and 98 deletions

View File

@ -5,7 +5,7 @@
versionName="0.0.4"> versionName="0.0.4">
<uses-sdk android:minSdkVersion="16"/> <uses-sdk android:minSdkVersion="16"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<application android:label="Tilde Friends" android:usesCleartextTraffic="true"> <application android:label="Tilde Friends" android:usesCleartextTraffic="true" android:debuggable="true">
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>

View File

@ -53,7 +53,11 @@ public class MainActivity extends Activity {
Log.w("tildefriends", e.toString()); Log.w("tildefriends", e.toString());
} }
ProcessBuilder builder = new ProcessBuilder(getFilesDir().toString() + "/tildefriends", "run", "-z", getPackageResourcePath().toString()); ProcessBuilder builder = new ProcessBuilder(
getFilesDir().toString() + "/tildefriends",
"run",
"-z",
getPackageResourcePath().toString());
Log.w("tildefriends", "files = " + getFilesDir().toString()); Log.w("tildefriends", "files = " + getFilesDir().toString());
builder.directory(getFilesDir()); builder.directory(getFilesDir());
builder.inheritIO(); builder.inheritIO();
@ -83,8 +87,4 @@ public class MainActivity extends Activity {
web.loadUrl("http://127.0.0.1:12345/~core/apps/"); web.loadUrl("http://127.0.0.1:12345/~core/apps/");
} }
static {
System.loadLibrary("tildefriends");
}
} }

View File

@ -2,6 +2,8 @@
#include "log.h" #include "log.h"
#include "mem.h" #include "mem.h"
#include "ssb.h"
#include "task.h"
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
@ -31,7 +33,7 @@ static JSValue _database_get_all(JSContext* context, JSValueConst this_val, int
static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data); static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data);
void tf_database_register(JSContext* context, sqlite3* sqlite) void tf_database_register(JSContext* context)
{ {
JS_NewClassID(&_database_class_id); JS_NewClassID(&_database_class_id);
JSClassDef def = JSClassDef def =
@ -45,13 +47,12 @@ void tf_database_register(JSContext* context, sqlite3* sqlite)
} }
JSValue global = JS_GetGlobalObject(context); JSValue global = JS_GetGlobalObject(context);
JSValue data[] = { JS_NewInt64(context, (int64_t)(intptr_t)sqlite) }; JSValue constructor = JS_NewCFunctionData(context, _database_create, 0, 0, 0, NULL);
JSValue constructor = JS_NewCFunctionData(context, _database_create, 0, 0, 1, data);
JS_SetConstructorBit(context, constructor, true); JS_SetConstructorBit(context, constructor, true);
JS_SetPropertyStr(context, global, "Database", constructor); JS_SetPropertyStr(context, global, "Database", constructor);
JSValue databases = JS_NewObject(context); JSValue databases = JS_NewObject(context);
JS_SetPropertyStr(context, global, "databases", databases); JS_SetPropertyStr(context, global, "databases", databases);
JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 0, 0, 1, data)); JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 0, 0, 0, NULL));
JS_FreeValue(context, global); JS_FreeValue(context, global);
} }
@ -60,9 +61,8 @@ static JSValue _database_create(JSContext* context, JSValueConst this_val, int a
++_database_count; ++_database_count;
JSValue object = JS_NewObjectClass(context, _database_class_id); JSValue object = JS_NewObjectClass(context, _database_class_id);
int64_t value = 0; tf_task_t* task = tf_task_get(context);
JS_ToInt64(context, &value, data[0]); sqlite3* db = tf_ssb_get_db(tf_task_get_ssb(task));
sqlite3* db = (sqlite3*)(intptr_t)value;
database_t* database = tf_malloc(sizeof(database_t)); database_t* database = tf_malloc(sizeof(database_t));
*database = (database_t) *database = (database_t)
@ -105,7 +105,7 @@ static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc
if (database) if (database)
{ {
sqlite3_stmt* statement; sqlite3_stmt* statement;
if (sqlite3_prepare(database->db, "SELECT value FROM properties WHERE id = $1 AND key = $2", -1, &statement, NULL) == SQLITE_OK) if (sqlite3_prepare(database->db, "SELECT value FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
{ {
size_t length; size_t length;
const char* keyString = JS_ToCStringLen(context, &length, argv[0]); const char* keyString = JS_ToCStringLen(context, &length, argv[0]);
@ -278,9 +278,8 @@ JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc,
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data) static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{ {
int64_t value = 0; tf_task_t* task = tf_task_get(context);
JS_ToInt64(context, &value, data[0]); sqlite3* db = tf_ssb_get_db(tf_task_get_ssb(task));
sqlite3* db = (sqlite3*)(intptr_t)value;
JSValue array = JS_UNDEFINED; JSValue array = JS_UNDEFINED;
sqlite3_stmt* statement; sqlite3_stmt* statement;

View File

@ -2,6 +2,4 @@
#include "quickjs.h" #include "quickjs.h"
typedef struct sqlite3 sqlite3; void tf_database_register(JSContext* context);
void tf_database_register(JSContext* context, sqlite3* sqlite);

View File

@ -1,23 +0,0 @@
#if defined(__ANDROID__)
#include "log.h"
#include <jni.h>
#include <unistd.h>
void tf_android(const char* apk_path);
JNIEXPORT void JNICALL Java_com_unprompted_tildefriends_MainActivity_setFilesPath(JNIEnv* env, jobject obj, jstring files_path, jstring apk_path)
{
tf_printf("setFilesPath called.\n");
const char* path_string = (*env)->GetStringUTFChars(env, files_path, NULL);
tf_printf("files_path = %s\n", path_string);
int result = chdir(path_string);
tf_printf("chdir %s => %d\n", path_string, result);
(*env)->ReleaseStringUTFChars(env, files_path, path_string);
const char* apk_path_string = (*env)->GetStringUTFChars(env, apk_path, NULL);
tf_printf("apk = %s\n", apk_path_string);
tf_android(apk_path_string);
(*env)->ReleaseStringUTFChars(env, apk_path, apk_path_string);
}
#endif

View File

@ -745,19 +745,3 @@ done:
tf_mem_shutdown(); tf_mem_shutdown();
return result; return result;
} }
static void _tf_android_thread(void* user_data)
{
char* apk_path = user_data;
char* args[] = { "tildefriends", "run", "-z", apk_path };
main(_countof(args), args);
tf_free(apk_path);
}
void tf_android(const char* apk_path)
{
uv_thread_t thread_id = 0;
tf_printf("Starting a thread.\n");
uv_thread_create(&thread_id, _tf_android_thread, tf_strdup(apk_path));
tf_printf("Post-thread start.\n");
}

View File

@ -352,7 +352,7 @@ void tf_ssb_import_from_zip(tf_ssb_t* ssb, const char* zip_path, const char* use
unzFile zip = unzOpen(zip_path); unzFile zip = unzOpen(zip_path);
if (zip) if (zip)
{ {
tf_printf("Importing from %s.", zip_path); tf_printf("Importing from %s.\n", zip_path);
if (unzGoToFirstFile(zip) == UNZ_OK) if (unzGoToFirstFile(zip) == UNZ_OK)
{ {
while (unzGoToNextFile(zip) == UNZ_OK) while (unzGoToNextFile(zip) == UNZ_OK)

View File

@ -1584,44 +1584,11 @@ static void _tf_task_trace_to_parent(tf_trace_t* trace, const char* buffer, size
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskTrace, buffer, size); tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskTrace, buffer, size);
} }
#if defined(__ANDROID__)
static void _tf_task_extract_file(tf_task_t* task, const char* name, int mode)
{
size_t size = 0;
char path_in_zip[256];
snprintf(path_in_zip, sizeof(path_in_zip), "lib/arm64-v8a/%s", name);
const char* exe_source = _task_loadFile(task, path_in_zip, &size);
if (exe_source)
{
FILE* exe_target = fopen(name, "wb");
if (exe_target)
{
fwrite(exe_source, size, 1, exe_target);
fchmod(fileno(exe_target), mode);
fclose(exe_target);
}
else
{
tf_printf("failed to open %s for write", name);
}
tf_free((void*)exe_source);
}
else
{
tf_printf("failed to read %s source", path_in_zip);
}
}
#endif
void tf_task_activate(tf_task_t* task) void tf_task_activate(tf_task_t* task)
{ {
assert(!task->_activated); assert(!task->_activated);
task->_activated = true; task->_activated = true;
#if defined(__ANDROID__)
_tf_task_extract_file(task, "tildefriends", 0755);
#endif
JSContext* context = task->_context; JSContext* context = task->_context;
JSValue global = JS_GetGlobalObject(context); JSValue global = JS_GetGlobalObject(context);
JSValue e = JS_NewObject(context); JSValue e = JS_NewObject(context);
@ -1679,7 +1646,7 @@ void tf_task_activate(tf_task_t* task)
JS_SetPropertyStr(context, global, "Socket", tf_socket_register(context)); JS_SetPropertyStr(context, global, "Socket", tf_socket_register(context));
JS_SetPropertyStr(context, global, "TlsContext", tf_tls_context_register(context)); JS_SetPropertyStr(context, global, "TlsContext", tf_tls_context_register(context));
tf_file_register(context); tf_file_register(context);
tf_database_register(context, task->_db); tf_database_register(context);
task->_ssb = tf_ssb_create(&task->_loop, task->_context, task->_db_path); task->_ssb = tf_ssb_create(&task->_loop, task->_context, task->_db_path);
tf_ssb_set_trace(task->_ssb, task->_trace); tf_ssb_set_trace(task->_ssb, task->_trace);

View File

@ -45,10 +45,6 @@ void tf_taskstub_startup()
JS_NewClassID(&_classId); JS_NewClassID(&_classId);
size_t size = sizeof(_executable); size_t size = sizeof(_executable);
uv_exepath(_executable, &size); uv_exepath(_executable, &size);
#if defined(__ANDROID__)
/* We have already changed into our files directory. */
snprintf(_executable, sizeof(_executable), "./tildefriends");
#endif
tf_printf("exepath is %s\n", _executable); tf_printf("exepath is %s\n", _executable);
initialized = true; initialized = true;
} }