Add localStorage.clear in JSB

This commit is contained in:
pandamicro 2015-05-12 18:02:14 +08:00
parent 1129949722
commit c9ee849a2c
4 changed files with 35 additions and 1 deletions

View File

@ -69,5 +69,15 @@ bool JSB_localStorageSetItem(JSContext *cx, uint32_t argc, jsval *vp) {
return true;
}
// Arguments: char*, char*
// Ret value: void
bool JSB_localStorageClear(JSContext *cx, uint32_t argc, jsval *vp) {
JSB_PRECONDITION2( argc == 0, cx, false, "Invalid number of arguments" );
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
localStorageClear();
args.rval().setUndefined();
return true;
}
//#endif // JSB_INCLUDE_SYSTEM

View File

@ -12,6 +12,7 @@ extern "C" {
bool JSB_localStorageGetItem(JSContext *cx, uint32_t argc, jsval *vp);
bool JSB_localStorageRemoveItem(JSContext *cx, uint32_t argc, jsval *vp);
bool JSB_localStorageSetItem(JSContext *cx, uint32_t argc, jsval *vp);
bool JSB_localStorageClear(JSContext *cx, uint32_t argc, jsval *vp);
#ifdef __cplusplus
}

View File

@ -42,6 +42,7 @@ static sqlite3 *_db;
static sqlite3_stmt *_stmt_select;
static sqlite3_stmt *_stmt_remove;
static sqlite3_stmt *_stmt_update;
static sqlite3_stmt *_stmt_clear;
static void localStorageCreateTable()
@ -80,6 +81,10 @@ void localStorageInit( const std::string& fullpath/* = "" */)
// DELETE
const char *sql_remove = "DELETE FROM data WHERE key=?;";
ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);
// Clear
const char *sql_clear = "DELETE FROM data;";
ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr);
if( ret != SQLITE_OK ) {
printf("Error initializing DB\n");
@ -130,11 +135,15 @@ bool localStorageGetItem( const std::string& key, std::string *outItem )
ok |= sqlite3_step(_stmt_select);
const unsigned char *text = sqlite3_column_text(_stmt_select, 0);
if( (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW) || !text)
if ( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW )
{
printf("Error in localStorage.getItem()\n");
return false;
}
else if (!text)
{
return false;
}
else
{
outItem->assign((const char*)text);
@ -157,4 +166,15 @@ void localStorageRemoveItem( const std::string& key )
printf("Error in localStorage.removeItem()\n");
}
/** removes all items from the LS */
void localStorageClear()
{
assert( _initialized );
int ok = sqlite3_step(_stmt_clear);
if( ok != SQLITE_OK && ok != SQLITE_DONE)
printf("Error in localStorage.clear()\n");
}
#endif // #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)

View File

@ -51,6 +51,9 @@ bool CC_DLL localStorageGetItem( const std::string& key, std::string *outItem );
/** Removes an item from the JS. */
void CC_DLL localStorageRemoveItem( const std::string& key );
/** Removes all items from the JS. */
void CC_DLL localStorageClear();
// end group
/// @}