Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 198 lines (154 sloc) 3.265 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
// HACK constants so that we use the 64bit version of stat
#define _DARWIN_USE_64_BIT_INODE 1
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64

#include "config.h"

#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef RBX_WINDOWS
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

#include "windows_compat.h"
#include "ffi_util.hpp"

extern "C" {

extern char** environ;

int ffi_errno() { return errno; }

char** ffi_environ() { return environ; }

void ffi_set_errno(int n) {
  errno = n;
}

#ifdef timezone
time_t ffi_timezone() {
  return timezone;
}
#else
// try FreeBSD extensions to struct tm
time_t ffi_timezone() {
  struct tm *lt;
  time_t t;

  t = time(NULL);
  lt = localtime(&t);

  return lt->tm_gmtoff;
}
#endif

char* ffi_tzname(int dst) {
  if(dst) {
    return tzname[1];
  } else {
    return tzname[0];
  }
}

/*
int ffi_daylight() {
return daylight;
}
*/

uintptr_t ffi_address(void *ptr) {
  return (uintptr_t)ptr;
}

int ffi_write_int(int *ptr, int val) {
  *ptr = val;
  return val;
}

int ffi_read_int(int *ptr) {
  return *ptr;
}

long ffi_write_long(long *ptr, long val) {
  *ptr = val;
  return val;
}

long ffi_read_long(long *ptr) {
  return *ptr;
}

double ffi_write_float(double *ptr, double val) {
  *ptr = val;
  return val;
}

double ffi_read_float(double *ptr) {
  return *ptr;
}

char *ffi_read_string(char *ptr) {
  return ptr;
}

void *ffi_read_pointer(void **ptr) {
  return *ptr;
}

void *ffi_add_ptr(char *ptr, int offset) {
  return (void*)(ptr + offset);
}

int ffi_type_size(int type) {
  switch(type) {
    case RBX_FFI_TYPE_CHAR:
    case RBX_FFI_TYPE_UCHAR:
    case RBX_FFI_TYPE_BOOL:
    return sizeof(char);

    case RBX_FFI_TYPE_SHORT:
    case RBX_FFI_TYPE_USHORT:
    return sizeof(short);

    case RBX_FFI_TYPE_INT:
    case RBX_FFI_TYPE_UINT:
    return sizeof(int);

    case RBX_FFI_TYPE_LONG:
    case RBX_FFI_TYPE_ULONG:
    return sizeof(long);

    case RBX_FFI_TYPE_LONG_LONG:
    case RBX_FFI_TYPE_ULONG_LONG:
    return sizeof(long long);

    case RBX_FFI_TYPE_FLOAT:
    return sizeof(float);

    case RBX_FFI_TYPE_DOUBLE:
    return sizeof(double);

    case RBX_FFI_TYPE_PTR:
    case RBX_FFI_TYPE_STRING:
    case RBX_FFI_TYPE_STATE:
    case RBX_FFI_TYPE_STRPTR:
    case RBX_FFI_TYPE_OBJECT:
    return sizeof(void*);

    default:

    return -1;
  }
}

int ffi_cb_test(int (*thing)(int)) {
  return (*thing)(42);
}

unsigned int ffi_cast(unsigned int val) {
  return val;
}

#ifndef major
#define major(x) x
#endif

#ifndef minor
#define minor(x) 0
#endif

long ffi_major(dev_t n) {
  return major(n);
}

long ffi_minor(dev_t n) {
  return minor(n);
}

/* FIXME: these are TEMPORARY until we determine how to
* have FFI resolve symbols that may be macros. This is
* used rather than a primitive so that it is easier to
* replace (unlike primitives).
*/
int ffi_stat(const char *path, struct stat *buf) {
  return stat(path, buf);
}

int ffi_fstat(int filedes, struct stat *buf) {
  return fstat(filedes, buf);
}

#ifndef RBX_WINDOWS
int ffi_lstat(const char *path, struct stat *buf) {
  return lstat(path, buf);
}
#endif

}
Something went wrong with that request. Please try again.