/*static*/ int CStdlibClass::setenv(ScriptObject* self, Stringp name, Stringp value, bool overwrite) { Toplevel* toplevel = self->toplevel(); if( !name ) { toplevel->throwArgumentError(kNullArgumentError, "name"); } if( !value ) { toplevel->throwArgumentError(kNullArgumentError, "value"); } int writeover = 0; if( overwrite ) { writeover = 1; } #if AVMSYSTEM_WIN32 StUTF16String nameUTF16(name); StUTF16String valueUTF16(value); return VMPI_setenv16( nameUTF16.c_str(), valueUTF16.c_str(), writeover ); #else StUTF8String nameUTF8(name); StUTF8String valueUTF8(value); return VMPI_setenv( nameUTF8.c_str(), valueUTF8.c_str(), writeover ); #endif }
/*static*/ int CSysStatClass::fstat(ScriptObject* self, int fildes, CStatusObject* buf) { Toplevel* toplevel = self->toplevel(); if (!buf) { toplevel->throwArgumentError(kNullArgumentError, "buf"); } struct stat statbuf; int result = VMPI_fstat( fildes, &statbuf ); if( result != -1 ) { buf->set_st_dev( statbuf.st_dev ); buf->set_st_ino( statbuf.st_ino ); buf->set_st_mode( statbuf.st_mode ); buf->set_st_nlink( statbuf.st_nlink ); buf->set_st_uid( statbuf.st_uid ); buf->set_st_gid( statbuf.st_gid ); buf->set_st_rdev( statbuf.st_rdev ); buf->set_st_size( (double) statbuf.st_size ); buf->set_st_atime( (double) statbuf.st_atime ); buf->set_st_mtime( (double) statbuf.st_mtime ); buf->set_st_ctime( (double) statbuf.st_ctime ); } return result; }
/*static*/ int CStdlibClass::putenv(ScriptObject* self, Stringp name) { Toplevel* toplevel = self->toplevel(); if( !name ) { toplevel->throwArgumentError(kNullArgumentError, "name"); } #if AVMSYSTEM_WIN32 StUTF16String nameUTF16(name); wchar * str = VMPI_strdup16( nameUTF16.c_str() ); int result = VMPI_putenv16( str ); #else StUTF8String nameUTF8(name); char * str = VMPI_strdup( nameUTF8.c_str() ); int result = VMPI_putenv( str ); #endif /* note: do not free() after strdup() or the string ref will be lost so yeah it create a small memory leak need to investigate if AVM2 intern string can solve this or maybe create a special string pool to save such ref that we could clean before the VM exit */ //VMPI_free(str); return result; }
/*static*/ CdirentObject* CDirentClass::readdir(ScriptObject* self, CDIRObject* dirp) { Toplevel* toplevel = self->toplevel(); if( !dirp ) { toplevel->throwArgumentError(kNullArgumentError, "dirp"); } dirent *entry = VMPI_readdir( dirp->read() ); if( entry ) { ShellToplevel* shelltop = (ShellToplevel*)self->toplevel(); CdirentClass *direc = shelltop->getShellClasses()->get_direntClass(); CdirentObject *direo = direc->constructObject(); direo->write( entry ); AvmCore *core = self->core(); direo->set_d_ino( entry->d_ino ); direo->set_d_name( core->newStringUTF8( entry->d_name ) ); return direo; } return NULL; }
/*static*/ ChostentObject* CNetdbClass::gethostbyname(ScriptObject* self, Stringp name) { AvmCore *core = self->core(); Toplevel* toplevel = self->toplevel(); if( !name ) { toplevel->throwArgumentError(kNullArgumentError, "name"); } struct hostent *he; StUTF8String nameUTF8(name); he = VMPI_gethostbyname( nameUTF8.c_str() ); if( he ) { ShellToplevel* shelltop = (ShellToplevel*)self->toplevel(); ChostentClass *hc = shelltop->getShellClasses()->get_hostentClass(); ChostentObject *ho = hc->constructObject(); ho->set_h_name( core->newStringUTF8( he->h_name ) ); ArrayObject *aliases = toplevel->arrayClass()->newArray(); int count = 0; int i; for( i=0; he->h_aliases[i] != NULL; ++i ) { aliases->setUintProperty( count++, core->newStringUTF8( he->h_aliases[i] )->atom() ); } ho->set_h_aliases( aliases ); ho->set_h_addrtype( he->h_addrtype ); ho->set_h_length( he->h_length ); ArrayObject *addrlist = toplevel->arrayClass()->newArray(); count = 0; for( i=0; he->h_addr_list[i] != NULL; ++i ) { struct in_addr in; memcpy(&in.s_addr, he->h_addr_list[i], sizeof (in.s_addr)); CIn_AddrClass *ac = shelltop->getShellClasses()->get_in_addrClass(); CIn_AddrObject *ao = ac->constructObject(); ao->set_s_addr( in.s_addr ); addrlist->setUintProperty( count++, ao->toAtom() ); //addrlist->setUintProperty( count++, core->newStringUTF8( inet_ntoa(in) )->atom() ); } ho->set_h_addr_list( addrlist ); return ho; } return NULL; }
Stringp FileClass::read(Stringp filename) { Toplevel* toplevel = this->toplevel(); AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } StUTF8String filenameUTF8(filename); File* fp = Platform::GetInstance()->createFile(); if(!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_READ)) { if(fp) { Platform::GetInstance()->destroyFile(fp); } toplevel->throwError(kFileOpenError, filename); } int64_t fileSize = fp->size(); if(fileSize >= (int64_t)INT32_T_MAX) //File APIs cannot handle files > 2GB { toplevel->throwRangeError(kOutOfRangeError, filename); } int len = (int)fileSize; MMgc::GC::AllocaAutoPtr _c; union { uint8_t* c; wchar* c_w; }; c = (uint8_t*)VMPI_alloca(core, _c, len+1); len = (int)fp->read(c, len); //need to force since the string creation functions expect an int c[len] = 0; fp->close(); Platform::GetInstance()->destroyFile(fp); if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { return core->newStringUTF8((const char*)c + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; return core->newStringEndianUTF16(/*littleEndian*/false, c_w, len); } else if ((c[0] == 0xff) && (c[1] == 0xfe))
// this = argv[0] // arg1 = argv[1] // argN = argv[argc] Atom ClassClosure::call(int argc, Atom* argv) { Toplevel* toplevel = this->toplevel(); // explicit coercion of a class object. if (argc != 1) { toplevel->throwArgumentError(kCoerceArgumentCountError, toplevel->core()->toErrorString(argc)); } return toplevel->coerce(argv[1], (Traits*)ivtable()->traits); }
Stringp FileClass::read(Stringp filename) { Toplevel* toplevel = this->toplevel(); AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } StUTF8String filenameUTF8(filename); File* fp = Platform::GetInstance()->createFile(); if(!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_READ)) { if(fp) { Platform::GetInstance()->destroyFile(fp); } toplevel->throwError(kFileOpenError, filename); } int64_t fileSize = fp->size(); if(fileSize >= (int64_t)INT32_T_MAX) //File APIs cannot handle files > 2GB { toplevel->throwRangeError(kOutOfRangeError, filename); } int len = (int)fileSize; // Avoid VMPI_alloca - the buffer can be large and the memory is non-pointer-containing, // but the GC will scan it conservatively. uint8_t* c = (uint8_t*)core->gc->Alloc(len+1); len = (int)fp->read(c, len); //need to force since the string creation functions expect an int c[len] = 0; fp->close(); Platform::GetInstance()->destroyFile(fp); Stringp ret = NULL; if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { ret = core->newStringUTF8((const char*)c + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; ret = core->newStringEndianUTF16(/*littleEndian*/false, (wchar*)(void*)c, len); } else if ((c[0] == 0xff) && (c[1] == 0xfe))
/*static*/ int CSysSelectClass::select(ScriptObject* self, int nfds, Cfd_setObject* readfds, Cfd_setObject* writefds, Cfd_setObject* errorfds, CtimevalObject* timeout) { Toplevel* toplevel = self->toplevel(); if( !timeout ) { toplevel->throwArgumentError(kNullArgumentError, "timeout"); } struct timeval tv; tv = timeout->toStruct(); int result = -1; if( !readfds && !writefds && !errorfds ) { result = VMPI_select( nfds, NULL, NULL, NULL, &tv ); } else if( !writefds && !errorfds ) { result = VMPI_select( nfds, &readfds->fds, NULL, NULL, &tv ); } else if( !errorfds ) { result = VMPI_select( nfds, &readfds->fds, &writefds->fds, NULL, &tv ); } else if( !writefds ) { result = VMPI_select( nfds, &readfds->fds, NULL, &errorfds->fds, &tv ); } else if( !readfds ) { result = VMPI_select( nfds, NULL, &writefds->fds, &errorfds->fds, &tv ); } else if( !readfds && !writefds ) { result = VMPI_select( nfds, NULL, NULL, &errorfds->fds, &tv ); } else if( !readfds && !errorfds ) { result = VMPI_select( nfds, NULL, &writefds->fds, NULL, &tv ); } else { result = VMPI_select( nfds, &readfds->fds, &writefds->fds, &errorfds->fds, &tv ); } if( result != -1 ) { timeout->fromStruct( tv ); } return result; }
/*static*/ void CDirentClass::seekdir(ScriptObject* self, CDIRObject* dirp, double loc) { Toplevel* toplevel = self->toplevel(); if( !dirp ) { toplevel->throwArgumentError(kNullArgumentError, "dirp"); } VMPI_seekdir( dirp->read(), (off_t) loc ); }
/*static*/ void CSysSelectClass::_avm_FD_ZERO(ScriptObject* self, Cfd_setObject* fdsetp) { Toplevel* toplevel = self->toplevel(); if( !fdsetp ) { toplevel->throwArgumentError(kNullArgumentError, "fdsetp"); } VMPI_FD_ZERO( &fdsetp->fds ); }
/*static*/ double CDirentClass::telldir(ScriptObject* self, CDIRObject* dirp) { Toplevel* toplevel = self->toplevel(); if( !dirp ) { toplevel->throwArgumentError(kNullArgumentError, "dirp"); } return (double)VMPI_telldir( dirp->read() ); }
/*static*/ int CDirentClass::dirfd(ScriptObject* self, CDIRObject* dirp) { Toplevel* toplevel = self->toplevel(); if( !dirp ) { toplevel->throwArgumentError(kNullArgumentError, "dirp"); } return VMPI_dirfd( dirp->read() ); }
/*static*/ void CDirentClass::rewinddir(ScriptObject* self, CDIRObject* dirp) { Toplevel* toplevel = self->toplevel(); if( !dirp ) { toplevel->throwArgumentError(kNullArgumentError, "dirp"); } VMPI_rewinddir( dirp->read() ); }
/*static*/ int CStdlibClass::mkstemp(ScriptObject* self, Stringp templ) { Toplevel* toplevel = self->toplevel(); if( !templ ) { toplevel->throwArgumentError(kNullArgumentError, "template"); } StUTF8String templateUTF8(templ); return VMPI_mkstemp( (char*)templateUTF8.c_str() ); }
/*static*/ double CStdlibClass::atol(ScriptObject* self, Stringp str) { Toplevel* toplevel = self->toplevel(); if (!str) { toplevel->throwArgumentError(kNullArgumentError, "str"); } StUTF8String strUTF8(str); return VMPI_atol( strUTF8.c_str() ); }
/*static*/ int CStdlibClass::mblen(ScriptObject* self, Stringp s, int i) { Toplevel* toplevel = self->toplevel(); if( !s ) { toplevel->throwArgumentError(kNullArgumentError, "s"); } StUTF8String sUTF8(s); return VMPI_mblen( sUTF8.c_str(), i ); }
/*static*/ int CSysStatClass::stat(ScriptObject* self, Stringp path, CStatusObject* buf) { Toplevel* toplevel = self->toplevel(); if (!path) { toplevel->throwArgumentError(kNullArgumentError, "path"); } if (!buf) { toplevel->throwArgumentError(kNullArgumentError, "buf"); } struct stat statbuf; #if AVMSYSTEM_WIN32 StUTF16String pathUTF16(path); int result = VMPI_stat16( pathUTF16.c_str(), &statbuf ); #else StUTF8String pathUTF8(path); int result = VMPI_stat( pathUTF8.c_str(), &statbuf ); #endif if( result != -1 ) { buf->set_st_dev( statbuf.st_dev ); buf->set_st_ino( statbuf.st_ino ); buf->set_st_mode( statbuf.st_mode ); buf->set_st_nlink( statbuf.st_nlink ); buf->set_st_uid( statbuf.st_uid ); buf->set_st_gid( statbuf.st_gid ); buf->set_st_rdev( statbuf.st_rdev ); buf->set_st_size( (double) statbuf.st_size ); buf->set_st_atime( (double) statbuf.st_atime ); buf->set_st_mtime( (double) statbuf.st_mtime ); buf->set_st_ctime( (double) statbuf.st_ctime ); } return result; }
/*static*/ int CSysSelectClass::_avm_FD_ISSET(ScriptObject* self, int fd, Cfd_setObject* fdsetp) { Toplevel* toplevel = self->toplevel(); if( !fdsetp ) { toplevel->throwArgumentError(kNullArgumentError, "fdsetp"); } int result = VMPI_FD_ISSET( fd, &fdsetp->fds ); return result; }
Stringp FileClass::read(Stringp filename) { Toplevel* toplevel = this->toplevel(); AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } UTF8String* filenameUTF8 = filename->toUTF8String(); FILE *fp = fopen(filenameUTF8->c_str(), "r"); if (fp == NULL) { toplevel->throwError(kFileOpenError, filename); } fseek(fp, 0L, SEEK_END); long len = ftell(fp); #ifdef UNDER_CE fseek (fp, 0L, SEEK_SET); #else rewind(fp); #endif unsigned char *c = new unsigned char[len+1]; len = (long)fread(c, 1, len, fp); c[len] = 0; fclose(fp); if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { return core->newString(((char *)c) + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; Stringp out = new (core->GetGC()) String(len); wchar *buffer = out->lockBuffer(); for (long i = 0; i < len; i++) { buffer[i] = (c[0] << 8) + c[1]; c += 2; } out->unlockBuffer(); return out; } else if ((c[0] == 0xff) && (c[1] == 0xfe))
/*static*/ int CSysStatClass::chmod(ScriptObject* self, Stringp path, int mode) { Toplevel* toplevel = self->toplevel(); if (!path) { toplevel->throwArgumentError(kNullArgumentError, "path"); } #if AVMSYSTEM_WIN32 StUTF16String pathUTF16(path); return VMPI_chmod16( pathUTF16.c_str(), mode ); #else StUTF8String pathUTF8(path); return VMPI_chmod( pathUTF8.c_str(), mode ); #endif }
/*static*/ int CStdlibClass::unsetenv(ScriptObject* self, Stringp name) { Toplevel* toplevel = self->toplevel(); if( !name ) { toplevel->throwArgumentError(kNullArgumentError, "name"); } #if AVMSYSTEM_WIN32 StUTF16String nameUTF16(name); return VMPI_unsetenv16( nameUTF16.c_str() ); #else StUTF8String nameUTF8(name); return VMPI_unsetenv( nameUTF8.c_str() ); #endif }
/*static*/ Stringp CStdlibClass::mkdtemp(ScriptObject* self, Stringp templ) { Toplevel* toplevel = self->toplevel(); if( !templ ) { toplevel->throwArgumentError(kNullArgumentError, "template"); } StUTF8String templateUTF8(templ); char * str = VMPI_mkdtemp( (char*)templateUTF8.c_str() ); if( str ) { AvmCore *core = self->core(); return core->newStringUTF8( str ); } return NULL; }
/*static*/ Stringp CStdlibClass::getenv(ScriptObject* self, Stringp name) { AvmCore *core = self->core(); Toplevel* toplevel = self->toplevel(); if( !name ) { toplevel->throwArgumentError(kNullArgumentError, "name"); } #if AVMSYSTEM_WIN32 StUTF16String nameUTF16(name); const wchar * str = VMPI_getenv16( nameUTF16.c_str() ); Stringp value = core->newStringUTF16( str ); StUTF8String valueUTF8(value); return core->newStringUTF8( valueUTF8.c_str() ); #else StUTF8String nameUTF8(name); const char * str = VMPI_getenv( nameUTF8.c_str() ); return core->newStringUTF8( str ); #endif }
/*static*/ CDIRObject* CDirentClass::opendir(ScriptObject* self, Stringp dirname) { Toplevel* toplevel = self->toplevel(); if( !dirname ) { toplevel->throwArgumentError(kNullArgumentError, "dirname"); } StUTF8String dirnameUTF8(dirname); DIR *dir = VMPI_opendir( dirnameUTF8.c_str() ); if( dir ) { ShellToplevel* shelltop = (ShellToplevel*)self->toplevel(); CDIRClass *dirc = shelltop->getShellClasses()->get_DIRClass(); CDIRObject *diro = dirc->constructObject(); diro->write( dir ); return diro; } return NULL; }
/*static*/ Stringp CStdlibClass::realpath(ScriptObject* self, Stringp path) { AvmCore *core = self->core(); Toplevel* toplevel = self->toplevel(); if( !path ) { toplevel->throwArgumentError(kNullArgumentError, "path"); } #if AVMSYSTEM_WIN32 StUTF16String pathUTF16(path); wchar * str = VMPI_realpath16( pathUTF16.c_str() ); Stringp value = core->newStringUTF16( str ); StUTF8String valueUTF8(value); return core->newStringUTF8( valueUTF8.c_str() ); #else StUTF8String pathUTF8(path); char * str = VMPI_realpath( pathUTF8.c_str() ); return core->newStringUTF8( str ); #endif }
ChostentObject* CNetdbClass::gethostbyaddr4(CIn_AddrObject* addr, int type) { Toplevel* toplevel = this->toplevel(); AvmCore* core = toplevel->core(); if( !addr ) { toplevel->throwArgumentError(kNullArgumentError, "addr"); } struct hostent *he; struct in_addr ipv4addr; ipv4addr.s_addr = addr->get_s_addr(); //uint if( addr->call_isValid() == false ) { return NULL; } he = VMPI_gethostbyaddr( (char *)&ipv4addr, sizeof ipv4addr, type ); if( he ) { ShellToplevel* shelltop = (ShellToplevel*)this->toplevel(); ChostentClass *hc = shelltop->getShellClasses()->get_hostentClass(); ChostentObject *ho = hc->constructObject(); ho->set_h_name( core->newStringUTF8( he->h_name ) ); ArrayObject *aliases = toplevel->arrayClass()->newArray(); int count = 0; int i; for( i=0; he->h_aliases[i] != NULL; ++i ) { aliases->setUintProperty( count++, core->newStringUTF8( he->h_aliases[i] )->atom() ); } ho->set_h_aliases( aliases ); ho->set_h_addrtype( he->h_addrtype ); ho->set_h_length( he->h_length ); ArrayObject *addrlist = toplevel->arrayClass()->newArray(); count = 0; for( i=0; he->h_addr_list[i] != NULL; ++i ) { struct in_addr in; memcpy(&in.s_addr, he->h_addr_list[i], sizeof (in.s_addr)); CIn_AddrClass *ac = shelltop->getShellClasses()->get_in_addrClass(); CIn_AddrObject *ao = ac->constructObject(); ao->set_s_addr( in.s_addr ); addrlist->setUintProperty( count++, ao->toAtom() ); } ho->set_h_addr_list( addrlist ); return ho; } return NULL; }