| | 85 | ;;;;;;;;;;;;;;;;;;;;;;;;;;; PHP TYPES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| | 86 | ; |
|---|
| | 87 | ; Bigloo itself supports a variety of types, but the only ones we |
|---|
| | 88 | ; use in PHP land are: string, boolean, onum, php-hash and php-object |
|---|
| | 89 | ; |
|---|
| | 90 | ; You can use the normal bigloo functions for dealing with strings |
|---|
| | 91 | ; and booleans: |
|---|
| | 92 | ; |
|---|
| | 93 | ; string? |
|---|
| | 94 | ; boolean? |
|---|
| | 95 | ; make-string |
|---|
| | 96 | ; string=? |
|---|
| | 97 | ; substring |
|---|
| | 98 | ; ... etc |
|---|
| | 99 | ; |
|---|
| | 100 | ; Otherwise we have to do type juggling. If a bigloo type winds up |
|---|
| | 101 | ; in php land (such as a fixnum (bint) or #unspecified), it will |
|---|
| | 102 | ; show up strange when coerced to a string (i.e. ":ufo:") and may cause |
|---|
| | 103 | ; calculations to fail in general. |
|---|
| | 104 | ; |
|---|
| | 105 | ; "onum" is an "opaque number" and represents all numbers in php land. |
|---|
| | 106 | ; it follows zend's semantics for overflowing an integer into a float. you |
|---|
| | 107 | ; can use: onum-long? and onum-float? to see which it is |
|---|
| | 108 | ; |
|---|
| | 109 | ; You should use php versions of some normal scheme functions when |
|---|
| | 110 | ; dealing with php types. See runtime/php-types.scm and runtime/php-operators.scm |
|---|
| | 111 | ; |
|---|
| | 112 | ; Some commonly used functions: |
|---|
| | 113 | ; |
|---|
| | 114 | ; php-null? php-empty? php-hash? php-object? php-number? |
|---|
| | 115 | ; |
|---|
| | 116 | ; php-+ php-- php-= php-/ php-* php-% |
|---|
| | 117 | ; |
|---|
| | 118 | ; |
|---|
| | 119 | ; There are a few constants that should be used as well: |
|---|
| | 120 | ; |
|---|
| | 121 | ; NULL |
|---|
| | 122 | ; TRUE |
|---|
| | 123 | ; FALSE |
|---|
| | 124 | ; *zero* |
|---|
| | 125 | ; *one* |
|---|
| | 126 | ; |
|---|
| | 127 | ;;;;;;;;;;;; |
|---|
| | 128 | ; |
|---|
| | 129 | ; NOTE: You should coerce all input variables and return values to the required PHP type |
|---|
| | 130 | ; |
|---|
| | 131 | ; convert-to-number - returns an onum, may be long or float depending on input |
|---|
| | 132 | ; convert-to-float - returns an onum, forces float |
|---|
| | 133 | ; convert-to-integer - returns an onum, forces integer |
|---|
| | 134 | ; convert-to-boolean - returns boolean |
|---|
| | 135 | ; convert-to-string - returns string (also see mkstr) |
|---|
| | 136 | ; |
|---|
| | 137 | ;;;;; |
|---|
| | 138 | |
|---|
| | 139 | |
|---|
| | 140 | ;;;;;;;;;;;;;;;;;;;;;;; LOCAL FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| | 141 | ; |
|---|
| | 142 | ; Normal scheme procedures created with define are not callable by |
|---|
| | 143 | ; PHP scripts. |
|---|
| | 144 | ; |
|---|
| | 145 | |
|---|
| | 146 | ; |
|---|
| | 147 | ; here we have a scheme procedure that's run on init, which creates |
|---|
| | 148 | ; a builtin php object |
|---|
| | 149 | ; |
|---|
| | 150 | (define (create-skeleton-class) |
|---|
| | 151 | |
|---|
| | 152 | ; define a "builtin" class or interface. interfaces are simply abstract |
|---|
| | 153 | ; classes, but the 'interface flag should be used so the object system |
|---|
| | 154 | ; knows it is implementable |
|---|
| | 155 | ; |
|---|
| | 156 | ; a builtin class means it is always available while this extension is loaded, |
|---|
| | 157 | ; and it doesn't disappear at runtime reset |
|---|
| | 158 | |
|---|
| | 159 | ; note that for class/interface names, property names and method names, case |
|---|
| | 160 | ; will be kept but only properties are actually case sensitive |
|---|
| | 161 | |
|---|
| | 162 | ; class or interface defition. note that parent classes and interfaces should be defined |
|---|
| | 163 | ; first if the class will extend any |
|---|
| | 164 | (define-builtin-php-class 'Skeleton ; class name. a symbol |
|---|
| | 165 | '() ; extends, a list of one entry, a symbol |
|---|
| | 166 | '() ; implements, a list of symbols |
|---|
| | 167 | '() ; flags: abstract, interface, final |
|---|
| | 168 | ) |
|---|
| | 169 | |
|---|
| | 170 | ; define a class property |
|---|
| | 171 | (define-php-property 'Skeleton ; the class name. a symbol. |
|---|
| | 172 | "message" ; the property name. a string. |
|---|
| | 173 | "default message" ; default value, a string or other php type (e.g. NULL) |
|---|
| | 174 | 'protected ; visibility, one of: public, private, protected |
|---|
| | 175 | #f ; static? |
|---|
| | 176 | ) |
|---|
| | 177 | |
|---|
| | 178 | ; define a php class method |
|---|
| | 179 | (define-php-method 'Skeleton ; the class name, a symbol. |
|---|
| | 180 | "__construct" ; method name, a string. |
|---|
| | 181 | '(public) ; flags: public, private, protected, final, abstract |
|---|
| | 182 | Skeleton:__construct ; scheme procedure that implements this method (see below) |
|---|
| | 183 | ) |
|---|
| | 184 | |
|---|
| | 185 | ) |
|---|
| | 186 | |
|---|
| | 187 | ; |
|---|
| | 188 | ; Skeleton::__construct |
|---|
| | 189 | ; |
|---|
| | 190 | ; This is the scheme procedure that implements the "__construct" method for |
|---|
| | 191 | ; the Skeleton class, as defined in define-php-method above. The procedure |
|---|
| | 192 | ; name is arbitrary, but the arguments must be handled as shown. The first |
|---|
| | 193 | ; parameter will always be $this, which will always be a php-object or NULL |
|---|
| | 194 | ; for static methods. |
|---|
| | 195 | ; |
|---|
| | 196 | ; optional-args is a list of possible arguments that were passed into the method. |
|---|
| | 197 | ; this method looks for two arguments, message and code. |
|---|
| | 198 | ; |
|---|
| | 199 | (define (Skeleton:__construct this-unboxed . optional-args) |
|---|
| | 200 | (let ((message '()) |
|---|
| | 201 | (code '())) |
|---|
| | 202 | ; do we have arguments passed in? |
|---|
| | 203 | (when (pair? optional-args) |
|---|
| | 204 | ; yes, the first is message. save it and shift the argument list. |
|---|
| | 205 | (set! message |
|---|
| | 206 | (maybe-unbox (car optional-args))) |
|---|
| | 207 | (set! optional-args (cdr optional-args))) |
|---|
| | 208 | ; do we have another argument? |
|---|
| | 209 | (when (pair? optional-args) |
|---|
| | 210 | ; yes, save it as code. we aren't looking for any more, |
|---|
| | 211 | ; so don't bother with a shift |
|---|
| | 212 | (set! code |
|---|
| | 213 | (maybe-unbox (car optional-args)))) |
|---|
| | 214 | ; |
|---|
| | 215 | ; if the arguments now have a value, set our local properties |
|---|
| | 216 | ; note that 'all means we don't do a visibility check |
|---|
| | 217 | ; |
|---|
| | 218 | (when message |
|---|
| | 219 | (php-object-property-set!/string this-unboxed "message" message 'all)) |
|---|
| | 220 | (when code |
|---|
| | 221 | (php-object-property-set!/string this-unboxed "code" code 'all)))) |
|---|
| | 222 | |
|---|
| | 223 | |
|---|
| | 224 | ;;;;;;;;;;;;;;; |
|---|
| | 225 | |
|---|
| | 226 | |
|---|
| | 227 | ; a little function to compute x^n. not we don't do type conversion |
|---|
| | 228 | ; here, we assume it's done by our defbuiltin caller. that means |
|---|
| | 229 | ; we're just using bigloo's fixnums here. we've added type annotation |
|---|
| | 230 | ; to that effect (::bint) |
|---|
| | 231 | (define (my-little-expt x::bint n::bint) |
|---|
| | 232 | (expt x n)) |
|---|
| | 233 | |
|---|
| | 234 | ;;;;;;;;;;;;;;;; PHP VISIBLE BUILTIN FUNCTIONS ;;;;;;;;;;;;;;;;;;; |
|---|
| | 235 | ; |
|---|
| | 236 | ; Functions created with the defbuiltin macro are visible to |
|---|
| | 237 | ; PHP scripts |
|---|
| | 238 | ; |
|---|
| | 239 | |
|---|