Permalink
Browse files

keep the parent session class as a property in all classes so the gc …

…cleans it up last. also clean up some warnings and other minor bug fixes.
  • Loading branch information...
1 parent a8594a2 commit ddad05eef243962809460e00047062cb5803228c @vilhelmk committed Oct 25, 2011
Showing with 76 additions and 46 deletions.
  1. +13 −8 album.c
  2. +7 −5 artist.c
  3. +6 −0 php_spotify.h
  4. +18 −9 playlist.c
  5. +12 −10 spotify.c
  6. +13 −8 track.c
  7. +7 −6 user.c
View
21 album.c
@@ -35,7 +35,7 @@ PHP_METHOD(SpotifyAlbum, __construct)
zval *parent;
sp_album *album;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &parent, &album) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &album) == FAILURE) {
return;
}
@@ -45,6 +45,8 @@ PHP_METHOD(SpotifyAlbum, __construct)
obj->album = album;
obj->albumbrowse = NULL;
+ zend_update_property(spotifyalbum_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);
+
sp_album_add_ref(obj->album);
}
@@ -88,13 +90,15 @@ PHP_METHOD(SpotifyAlbum, getYear)
PHP_METHOD(SpotifyAlbum, getArtist)
{
sp_artist *artist;
- zval temp;
+ zval temp, *spotifyobject;
spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spotifyobject = GET_THIS_PROPERTY(spotifyalbum_ce, "spotify");
+
artist = sp_album_artist(p->album);
object_init_ex(return_value, spotifyartist_ce);
- SPOTIFY_METHOD2(SpotifyArtist, __construct, &temp, return_value, getThis(), artist);
+ SPOTIFY_METHOD2(SpotifyArtist, __construct, &temp, return_value, spotifyobject, artist);
}
PHP_METHOD(SpotifyAlbum, getNumTracks)
@@ -108,12 +112,14 @@ PHP_METHOD(SpotifyAlbum, getNumTracks)
PHP_METHOD(SpotifyAlbum, getTracks)
{
int num_tracks, i;
- zval tempretval, *thisptr = getThis();
+ zval tempretval, *thisptr = getThis(), *spotifyobject;
spotifyalbum_object *p = (spotifyalbum_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
SPOTIFY_METHOD(SpotifyAlbum, browse, &tempretval, thisptr);
+ spotifyobject = GET_THIS_PROPERTY(spotifyalbum_ce, "spotify");
+
array_init(return_value);
num_tracks = sp_albumbrowse_num_tracks(p->albumbrowse);
@@ -124,7 +130,7 @@ PHP_METHOD(SpotifyAlbum, getTracks)
zval *z_track;
ALLOC_INIT_ZVAL(z_track);
object_init_ex(z_track, spotifytrack_ce);
- SPOTIFY_METHOD2(SpotifyTrack, __construct, &tempretval, z_track, thisptr, track);
+ SPOTIFY_METHOD2(SpotifyTrack, __construct, &tempretval, z_track, spotifyobject, track);
add_next_index_zval(return_value, z_track);
}
}
@@ -194,7 +200,6 @@ zend_object_value spotifyalbum_create_handler(zend_class_entry *type TSRMLS_DC)
spotifyalbum_object *obj = (spotifyalbum_object *)emalloc(sizeof(spotifyalbum_object));
memset(obj, 0, sizeof(spotifyalbum_object));
- // obj->std.ce = type;
zend_object_std_init(&obj->std, type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &type->default_properties,
@@ -213,11 +218,11 @@ void spotify_init_album(TSRMLS_D)
INIT_CLASS_ENTRY(ce, "SpotifyAlbum", spotifyalbum_methods);
spotifyalbum_ce = zend_register_internal_class(&ce TSRMLS_CC);
spotifyalbum_ce->create_object = spotifyalbum_create_handler;
- //memcpy(&spotify_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- //spotify_object_handlers.clone_obj = NULL;
zend_declare_class_constant_long(spotifyalbum_ce, "TYPE_ALBUM", strlen("TYPE_ALBUM"), 0 TSRMLS_CC);
zend_declare_class_constant_long(spotifyalbum_ce, "TYPE_SINGLE", strlen("TYPE_SINGLE"), 1 TSRMLS_CC);
zend_declare_class_constant_long(spotifyalbum_ce, "TYPE_COMPILATION", strlen("TYPE_COMPILATION"), 2 TSRMLS_CC);
zend_declare_class_constant_long(spotifyalbum_ce, "TYPE_UNKNOWN", strlen("TYPE_UNKNOWN"), 3 TSRMLS_CC);
+
+ zend_declare_property_null(spotifyalbum_ce, "spotify", strlen("spotify"), ZEND_ACC_PROTECTED TSRMLS_CC);
}
View
@@ -33,7 +33,7 @@ PHP_METHOD(SpotifyArtist, __construct)
zval *parent;
sp_artist *artist;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &parent, &artist) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &artist) == FAILURE) {
return;
}
@@ -42,6 +42,8 @@ PHP_METHOD(SpotifyArtist, __construct)
obj->session = p->session;
obj->artist = artist;
+ zend_update_property(spotifyartist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);
+
sp_artist_add_ref(obj->artist);
}
@@ -62,7 +64,8 @@ PHP_METHOD(SpotifyArtist, getName)
PHP_METHOD(SpotifyArtist, getURI)
{
char uri[256];
- spotifyartist_object *p = (spotifyartist_object*)zend_object_get_object(getThis() TSRMLS_CC);
+ zval *object = getThis();
+ spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC);
sp_link *link = sp_link_create_from_artist(p->artist);
sp_link_as_string(link, uri, 256);
@@ -101,7 +104,6 @@ zend_object_value spotifyartist_create_handler(zend_class_entry *type TSRMLS_DC)
spotifyartist_object *obj = (spotifyartist_object *)emalloc(sizeof(spotifyartist_object));
memset(obj, 0, sizeof(spotifyartist_object));
- // obj->std.ce = type;
zend_object_std_init(&obj->std, type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &type->default_properties,
@@ -120,6 +122,6 @@ void spotify_init_artist(TSRMLS_D)
INIT_CLASS_ENTRY(ce, "SpotifyArtist", spotifyartist_methods);
spotifyartist_ce = zend_register_internal_class(&ce TSRMLS_CC);
spotifyartist_ce->create_object = spotifyartist_create_handler;
- //memcpy(&spotify_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- //spotify_object_handlers.clone_obj = NULL;
+
+ zend_declare_property_null(spotifyartist_ce, "spotify", strlen("spotify"), ZEND_ACC_PROTECTED TSRMLS_CC);
}
View
@@ -102,6 +102,9 @@ extern zend_class_entry *spotifyuser_ce;
extern void get_playlistcontainer_playlists(zval *return_value, container_browse_data *p, sp_playlistcontainer *pc);
+#define NOISY 0
+#define QUIET 1
+
#if ZEND_MODULE_API_NO >= 20090115
# define PUSH_PARAM(arg) zend_vm_stack_push(arg TSRMLS_CC)
# define POP_PARAM() (void)zend_vm_stack_pop(TSRMLS_C)
@@ -129,6 +132,9 @@ extern void get_playlistcontainer_playlists(zval *return_value, container_browse
POP_EO_PARAM(); \
POP_PARAM(); POP_PARAM();
+#define GET_THIS_PROPERTY(ce, name) \
+ zend_read_property(ce, getThis(), name, strlen(name), NOISY TSRMLS_CC);
+
/* push parameters, call function, pop parameters */
#define SPOTIFY_METHOD(classname, name, retval, thisptr) \
SPOTIFY_METHOD_BASE(classname, name)(0, retval, NULL, thisptr, 0 TSRMLS_CC);
View
@@ -43,6 +43,8 @@ PHP_METHOD(SpotifyPlaylist, __construct)
obj->session = p->session;
obj->playlist = playlist;
+ zend_update_property(spotifyplaylist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);
+
while (!sp_playlist_is_loaded(playlist)) {
sp_session_process_events(p->session, &timeout);
}
@@ -52,6 +54,8 @@ PHP_METHOD(SpotifyPlaylist, __construct)
PHP_METHOD(SpotifyPlaylist, __destruct)
{
+ spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ sp_playlist_release(p->playlist);
}
PHP_METHOD(SpotifyPlaylist, getName)
@@ -77,9 +81,11 @@ PHP_METHOD(SpotifyPlaylist, getURI)
PHP_METHOD(SpotifyPlaylist, getTracks)
{
int i, num_tracks, timeout = 0;
- zval *thisptr = getThis(), tempretval;
+ zval *thisptr = getThis(), tempretval, *parent, *spotifyobject;
spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
+ spotifyobject = zend_read_property(spotifyplaylist_ce, thisptr, "spotify", strlen("spotify"), NOISY TSRMLS_CC);
+
SPOTIFY_METHOD(SpotifyPlaylist, browse, &tempretval, thisptr);
array_init(return_value);
@@ -94,7 +100,7 @@ PHP_METHOD(SpotifyPlaylist, getTracks)
zval *z_track;
ALLOC_INIT_ZVAL(z_track);
object_init_ex(z_track, spotifytrack_ce);
- SPOTIFY_METHOD2(SpotifyTrack, __construct, &tempretval, z_track, thisptr, track);
+ SPOTIFY_METHOD2(SpotifyTrack, __construct, &tempretval, z_track, spotifyobject, track);
add_next_index_zval(return_value, z_track);
}
@@ -103,13 +109,15 @@ PHP_METHOD(SpotifyPlaylist, getTracks)
PHP_METHOD(SpotifyPlaylist, getOwner)
{
sp_user *user;
- zval temp;
+ zval temp, *spotifyobject;
spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ spotifyobject = zend_read_property(spotifyplaylist_ce, getThis(), "spotify", strlen("spotify"), NOISY TSRMLS_CC);
+
user = sp_playlist_owner(p->playlist);
object_init_ex(return_value, spotifyuser_ce);
- SPOTIFY_METHOD2(SpotifyUser, __construct, &temp, return_value, getThis(), user);
+ SPOTIFY_METHOD2(SpotifyUser, __construct, &temp, return_value, spotifyobject, user);
}
PHP_METHOD(SpotifyPlaylist, getDescription)
@@ -140,20 +148,22 @@ PHP_METHOD(SpotifyPlaylist, getTrackCreateTime)
PHP_METHOD(SpotifyPlaylist, getTrackCreator)
{
int index;
- zval *thisptr = getThis(), tempretval;
+ zval *thisptr = getThis(), tempretval, *spotifyobject;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
return;
}
+ spotifyobject = zend_read_property(spotifyplaylist_ce, thisptr, "spotify", strlen("spotify"), NOISY TSRMLS_CC);
+
spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
sp_user *user = sp_playlist_track_creator(p->playlist, index);
if (!user) {
RETURN_FALSE;
}
object_init_ex(return_value, spotifyuser_ce);
- SPOTIFY_METHOD2(SpotifyUser, __construct, &tempretval, return_value, thisptr, user);
+ SPOTIFY_METHOD2(SpotifyUser, __construct, &tempretval, return_value, spotifyobject, user);
}
PHP_METHOD(SpotifyPlaylist, isCollaborative)
@@ -309,7 +319,6 @@ zend_object_value spotifyplaylist_create_handler(zend_class_entry *type TSRMLS_D
spotifyplaylist_object *obj = (spotifyplaylist_object *)emalloc(sizeof(spotifyplaylist_object));
memset(obj, 0, sizeof(spotifyplaylist_object));
- // obj->std.ce = type;
zend_object_std_init(&obj->std, type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &type->default_properties,
@@ -328,6 +337,6 @@ void spotify_init_playlist(TSRMLS_D)
INIT_CLASS_ENTRY(ce, "SpotifyPlaylist", spotifyplaylist_methods);
spotifyplaylist_ce = zend_register_internal_class(&ce TSRMLS_CC);
spotifyplaylist_ce->create_object = spotifyplaylist_create_handler;
- //memcpy(&spotify_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- //spotify_object_handlers.clone_obj = NULL;
+
+ zend_declare_property_null(spotifyplaylist_ce, "spotify", strlen("spotify"), ZEND_ACC_PROTECTED TSRMLS_CC);
}
View
@@ -70,20 +70,20 @@ PHP_METHOD(Spotify, __construct)
config.user_agent = "libspotify-php";
if (VCWD_ACCESS(config.cache_location, W_OK|X_OK|R_OK) != 0) {
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "spotify.cache_location is not writable or readable", 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), "spotify.cache_location is not writable or readable", 0 TSRMLS_CC);
return;
}
if (VCWD_ACCESS(config.settings_location, W_OK|X_OK|R_OK) != 0) {
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "spotify.settings_location is not writable or readable", 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), "spotify.settings_location is not writable or readable", 0 TSRMLS_CC);
return;
}
key_filename = Z_STRVAL_P(z_key);
fp = fopen(key_filename, "rb");
if (!fp) {
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Unable to open spotify key file", 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), "Unable to open spotify key file", 0 TSRMLS_CC);
return;
}
@@ -93,14 +93,14 @@ PHP_METHOD(Spotify, __construct)
if (key_size > 4096) {
fclose(fp);
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Key file is way too large to be a key file", 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), "Key file is way too large to be a key file", 0 TSRMLS_CC);
return;
}
obj->key_data = (char*)emalloc(sizeof(char) * key_size);
if (fread(obj->key_data, 1, key_size, fp) != key_size) {
fclose(fp);
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Failed reading key file", 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), "Failed reading key file", 0 TSRMLS_CC);
return;
}
fclose(fp);
@@ -115,7 +115,7 @@ PHP_METHOD(Spotify, __construct)
if (SP_ERROR_OK != error) {
char *errMsg;
spprintf(&errMsg, 0, "Unable to create session: %s", sp_error_message(error));
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
return;
}
@@ -136,7 +136,7 @@ PHP_METHOD(Spotify, __destruct)
int timeout = 0;
if (obj->playlistcontainer != NULL) {
- //sp_playlistcontainer_release(obj->playlistcontainer);
+ sp_playlistcontainer_release(obj->playlistcontainer);
}
do {
@@ -322,6 +322,8 @@ PHP_METHOD(Spotify, initPlaylistContainer)
sp_session_process_events(p->session, &timeout);
}
+ sp_playlistcontainer_add_ref(p->playlistcontainer);
+
RETURN_TRUE;
}
@@ -332,11 +334,11 @@ static void logged_in(sp_session *session, sp_error error)
if (SP_ERROR_OK != error) {
p->is_logged_out = 1;
- //sp_session_release(session); // temp fix for crashing
+ sp_session_release(session);
char *errMsg;
spprintf(&errMsg, 0, "Login failed: %s", sp_error_message(error));
- zend_throw_exception((zend_class_entry*)zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
+ zend_throw_exception(zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
}
}
@@ -367,6 +369,7 @@ void spotify_free_storage(void *object TSRMLS_DC)
{
spotify_object *obj = (spotify_object*)object;
+ php_printf("releasing session...");
sp_session_release(obj->session);
zend_hash_destroy(obj->std.properties);
@@ -381,7 +384,6 @@ zend_object_value spotify_create_handler(zend_class_entry *type TSRMLS_DC)
spotify_object *obj = (spotify_object *)emalloc(sizeof(spotify_object));
memset(obj, 0, sizeof(spotify_object));
- // obj->std.ce = type;
zend_object_std_init(&obj->std, type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &type->default_properties,
Oops, something went wrong.

0 comments on commit ddad05e

Please sign in to comment.